agora inbox for pgsql-hackers@postgresql.org
help / color / mirror / Atom feedA micro-optimisation for walkdir()
131+ messages / 7 participants
[nested] [flat]
* A micro-optimisation for walkdir()
@ 2020-09-02 05:18 Thomas Munro <thomas.munro@gmail.com>
0 siblings, 1 reply; 131+ messages in thread
From: Thomas Munro @ 2020-09-02 05:18 UTC (permalink / raw)
To: pgsql-hackers
Hello hackers,
You don't need to call stat() just to find out if a dirent is a file
or directory, most of the time. Please see attached.
Attachments:
[text/x-patch] 0001-Skip-unnecessary-stat-calls-in-walkdir.patch (2.6K, ../../CA+hUKG+FzxupGGN4GpUdbzZN+tn6FQPHo8w0Q+APH5Wz8RG+ww@mail.gmail.com/2-0001-Skip-unnecessary-stat-calls-in-walkdir.patch)
download | inline diff:
From cc2f0fd4a078728a67d862e2deec0332fb8b3555 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Wed, 2 Sep 2020 16:15:09 +1200
Subject: [PATCH] Skip unnecessary stat() calls in walkdir().
Often the kernel has already told us whether we have a file or a
directory, so we can avoid a call to stat().
---
src/backend/storage/file/fd.c | 57 +++++++++++++++++++++++++++--------
1 file changed, 44 insertions(+), 13 deletions(-)
diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c
index f376a97ed6..99562f1ec7 100644
--- a/src/backend/storage/file/fd.c
+++ b/src/backend/storage/file/fd.c
@@ -3340,8 +3340,9 @@ walkdir(const char *path,
while ((de = ReadDirExtended(dir, path, elevel)) != NULL)
{
char subpath[MAXPGPATH * 2];
- struct stat fst;
- int sret;
+ bool is_unknown = false;
+ bool is_reg = false;
+ bool is_dir = false;
CHECK_FOR_INTERRUPTS();
@@ -3351,22 +3352,52 @@ walkdir(const char *path,
snprintf(subpath, sizeof(subpath), "%s/%s", path, de->d_name);
- if (process_symlinks)
- sret = stat(subpath, &fst);
- else
- sret = lstat(subpath, &fst);
+ /*
+ * We want to know if it's a file or directory. Some systems can tell
+ * us that already without an extra system call, but that's a BSD/GNU
+ * extension not mandated by POSIX, and even when the interface is
+ * present, sometimes the type is unknown, depending on the filesystem
+ * in use or in some cases options used at filesystem creation time.
+ */
+#if defined(DT_UNKNOWN) && defined(DT_REG) && defined(DT_DIR) && defined(DT_LNK)
+ if (de->d_type == DT_UNKNOWN ||
+ (de->d_type == DT_LNK && process_symlinks))
+ is_unknown = true;
+ else if (de->d_type == DT_REG)
+ is_reg = true;
+ else if (de->d_type == DT_DIR)
+ is_dir = true;
+#else
+ is_unknown = true;
+#endif
- if (sret < 0)
+ if (is_unknown)
{
- ereport(elevel,
- (errcode_for_file_access(),
- errmsg("could not stat file \"%s\": %m", subpath)));
- continue;
+ struct stat fst;
+ int sret;
+
+
+ if (process_symlinks)
+ sret = stat(subpath, &fst);
+ else
+ sret = lstat(subpath, &fst);
+
+ if (sret < 0)
+ {
+ ereport(elevel,
+ (errcode_for_file_access(),
+ errmsg("could not stat file \"%s\": %m", subpath)));
+ continue;
+ }
+ if (S_ISREG(fst.st_mode))
+ is_reg = true;
+ else if (S_ISDIR(fst.st_mode))
+ is_dir = true;
}
- if (S_ISREG(fst.st_mode))
+ if (is_reg)
(*action) (subpath, false, elevel);
- else if (S_ISDIR(fst.st_mode))
+ else if (is_dir)
walkdir(subpath, action, false, elevel);
}
--
2.20.1
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-02 14:34 Tom Lane <tgl@sss.pgh.pa.us>
parent: Thomas Munro <thomas.munro@gmail.com>
0 siblings, 1 reply; 131+ messages in thread
From: Tom Lane @ 2020-09-02 14:34 UTC (permalink / raw)
To: Thomas Munro <thomas.munro@gmail.com>; +Cc: pgsql-hackers
Thomas Munro <thomas.munro@gmail.com> writes:
> You don't need to call stat() just to find out if a dirent is a file
> or directory, most of the time. Please see attached.
Hm. If we do this, I can see wanting to apply the knowledge in more
places than walkdir(). Is it possible to extract out the nonstandard
bits into a reusable subroutine? I'm envisioning an API more or less
like
extern enum PGFileType identify_file_type(const char *path,
const struct dirent *de,
bool look_thru_symlinks);
regards, tom lane
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-02 15:51 Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 2 replies; 131+ messages in thread
From: Juan José Santamaría Flecha @ 2020-09-02 15:51 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers
On Wed, Sep 2, 2020 at 4:35 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Thomas Munro <thomas.munro@gmail.com> writes:
> > You don't need to call stat() just to find out if a dirent is a file
> > or directory, most of the time. Please see attached.
>
> Hm. If we do this, I can see wanting to apply the knowledge in more
> places than walkdir().
>
Win32 could also benefit from this micro-optimisation if we expanded the
dirent port to include d_type. Please find attached a patch that does so.
Regards,
Juan José Santamaría Flecha
Attachments:
[application/octet-stream] 0002-Add-d_type-to-WIN32-dirent-port.patch (2.0K, ../../CAC+AXB3urjeAOEAaU6Fb0uQsDhMYzmQpELkO9tMVW_ixU56pAw@mail.gmail.com/3-0002-Add-d_type-to-WIN32-dirent-port.patch)
download | inline diff:
diff --git a/src/include/port/win32_msvc/dirent.h b/src/include/port/win32_msvc/dirent.h
index 9fabdf3..1d85f95 100644
--- a/src/include/port/win32_msvc/dirent.h
+++ b/src/include/port/win32_msvc/dirent.h
@@ -10,6 +10,7 @@ struct dirent
{
long d_ino;
unsigned short d_reclen;
+ unsigned char d_type;
unsigned short d_namlen;
char d_name[MAX_PATH];
};
@@ -20,4 +21,26 @@ DIR *opendir(const char *);
struct dirent *readdir(DIR *);
int closedir(DIR *);
+/* File types for 'd_type'. */
+enum
+ {
+ DT_UNKNOWN = 0,
+# define DT_UNKNOWN DT_UNKNOWN
+ DT_FIFO = 1,
+# define DT_FIFO DT_FIFO
+ DT_CHR = 2,
+# define DT_CHR DT_CHR
+ DT_DIR = 4,
+# define DT_DIR DT_DIR
+ DT_BLK = 6,
+# define DT_BLK DT_BLK
+ DT_REG = 8,
+# define DT_REG DT_REG
+ DT_LNK = 10,
+# define DT_LNK DT_LNK
+ DT_SOCK = 12,
+# define DT_SOCK DT_SOCK
+ DT_WHT = 14
+# define DT_WHT DT_WHT
+ };
#endif
diff --git a/src/port/dirent.c b/src/port/dirent.c
index b264484..519dd82 100644
--- a/src/port/dirent.c
+++ b/src/port/dirent.c
@@ -69,6 +69,7 @@ opendir(const char *dirname)
d->handle = INVALID_HANDLE_VALUE;
d->ret.d_ino = 0; /* no inodes on win32 */
d->ret.d_reclen = 0; /* not used on win32 */
+ d->ret.d_type = DT_UNKNOWN;
return d;
}
@@ -77,6 +78,7 @@ struct dirent *
readdir(DIR *d)
{
WIN32_FIND_DATA fd;
+ DWORD attrib;
if (d->handle == INVALID_HANDLE_VALUE)
{
@@ -105,6 +107,19 @@ readdir(DIR *d)
}
strcpy(d->ret.d_name, fd.cFileName); /* Both strings are MAX_PATH long */
d->ret.d_namlen = strlen(d->ret.d_name);
+ /*
+ * The only identifed types are: directory, regular file or symbolic link.
+ * Errors are treated as a file type that could not be determined.
+ */
+ attrib = GetFileAttributes(d->ret.d_name);
+ if (attrib == INVALID_FILE_ATTRIBUTES)
+ d->ret.d_type = DT_UNKNOWN;
+ else if ((attrib & FILE_ATTRIBUTE_DIRECTORY) != 0)
+ d->ret.d_type = DT_DIR;
+ else if ((attrib & FILE_ATTRIBUTE_REPARSE_POINT) != 0)
+ d->ret.d_type = DT_LNK;
+ else
+ d->ret.d_type = DT_REG;
return &d->ret;
}
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-03 05:08 Thomas Munro <thomas.munro@gmail.com>
parent: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>
1 sibling, 1 reply; 131+ messages in thread
From: Thomas Munro @ 2020-09-03 05:08 UTC (permalink / raw)
To: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; pgsql-hackers
On Thu, Sep 3, 2020 at 3:52 AM Juan José Santamaría Flecha
<juanjo.santamaria@gmail.com> wrote:
> On Wed, Sep 2, 2020 at 4:35 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> Thomas Munro <thomas.munro@gmail.com> writes:
>> > You don't need to call stat() just to find out if a dirent is a file
>> > or directory, most of the time. Please see attached.
>>
>> Hm. If we do this, I can see wanting to apply the knowledge in more
>> places than walkdir().
Good idea. Here's a new version that defines a new function
get_dirent_type() in src/common/file_utils_febe.c and uses it for both
frontend and backend walkdir().
> Win32 could also benefit from this micro-optimisation if we expanded the dirent port to include d_type. Please find attached a patch that does so.
Is GetFileAttributes() actually faster than stat()? Oh, I see that
our pgwin32_safestat() makes extra system calls. Huh. Ok then.
Thanks!
Attachments:
[text/x-patch] v2-0001-Skip-unnecessary-stat-calls-in-walkdir.patch (8.5K, ../../CA+hUKGLwi2m7cnsYE=g+f99TAgLtWvq1eTpPVJWHHJFxVNxkGQ@mail.gmail.com/2-v2-0001-Skip-unnecessary-stat-calls-in-walkdir.patch)
download | inline diff:
From 19a5c980370f89d1340cdec7d74659715d8b4a17 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Thu, 3 Sep 2020 13:58:17 +1200
Subject: [PATCH v2 1/4] Skip unnecessary stat() calls in walkdir().
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Some kernels can tell us the type of a "dirent", so we can avoid a call
to stat() or lstat() in many cases. In order to be able to apply this
change to both frontend and backend versions of walkdir(), define a new
function get_dirent_type() in a new translation unit file_utils_febe.c.
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>
Discussion: https://postgr.es/m/CA%2BhUKG%2BFzxupGGN4GpUdbzZN%2Btn6FQPHo8w0Q%2BAPH5Wz8RG%2Bww%40mail.gmail.com
---
src/backend/storage/file/fd.c | 26 +++++------
src/common/Makefile | 1 +
src/common/file_utils.c | 28 +++++-------
src/common/file_utils_febe.c | 81 +++++++++++++++++++++++++++++++++
src/include/common/file_utils.h | 21 ++++++++-
src/tools/msvc/Mkvcbuild.pm | 2 +-
6 files changed, 126 insertions(+), 33 deletions(-)
create mode 100644 src/common/file_utils_febe.c
diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c
index f376a97ed6..b9acd38d23 100644
--- a/src/backend/storage/file/fd.c
+++ b/src/backend/storage/file/fd.c
@@ -89,6 +89,7 @@
#include "access/xlog.h"
#include "catalog/pg_tablespace.h"
#include "common/file_perm.h"
+#include "common/file_utils.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "portability/mem.h"
@@ -3340,8 +3341,6 @@ walkdir(const char *path,
while ((de = ReadDirExtended(dir, path, elevel)) != NULL)
{
char subpath[MAXPGPATH * 2];
- struct stat fst;
- int sret;
CHECK_FOR_INTERRUPTS();
@@ -3351,23 +3350,22 @@ walkdir(const char *path,
snprintf(subpath, sizeof(subpath), "%s/%s", path, de->d_name);
- if (process_symlinks)
- sret = stat(subpath, &fst);
- else
- sret = lstat(subpath, &fst);
-
- if (sret < 0)
+ switch (get_dirent_type(subpath, de, process_symlinks))
{
+ case PGFILETYPE_REG:
+ (*action) (subpath, false, elevel);
+ break;
+ case PGFILETYPE_DIR:
+ walkdir(subpath, action, false, elevel);
+ break;
+ case PGFILETYPE_ERROR:
ereport(elevel,
(errcode_for_file_access(),
errmsg("could not stat file \"%s\": %m", subpath)));
- continue;
+ break;
+ default:
+ break;
}
-
- if (S_ISREG(fst.st_mode))
- (*action) (subpath, false, elevel);
- else if (S_ISDIR(fst.st_mode))
- walkdir(subpath, action, false, elevel);
}
FreeDir(dir); /* we ignore any error here */
diff --git a/src/common/Makefile b/src/common/Makefile
index 16619e4ba8..aac92aabe1 100644
--- a/src/common/Makefile
+++ b/src/common/Makefile
@@ -56,6 +56,7 @@ OBJS_COMMON = \
exec.o \
f2s.o \
file_perm.o \
+ file_utils_febe.o \
hashfn.o \
ip.o \
jsonapi.o \
diff --git a/src/common/file_utils.c b/src/common/file_utils.c
index a2faafdf13..5a427e246c 100644
--- a/src/common/file_utils.c
+++ b/src/common/file_utils.c
@@ -2,7 +2,7 @@
*
* File-processing utility routines.
*
- * Assorted utility functions to work on files.
+ * Assorted utility functions to work on files, frontend only.
*
*
* Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
@@ -27,7 +27,6 @@
#include "common/file_utils.h"
#include "common/logging.h"
-
/* Define PG_FLUSH_DATA_WORKS if we have an implementation for pg_flush_data */
#if defined(HAVE_SYNC_FILE_RANGE)
#define PG_FLUSH_DATA_WORKS 1
@@ -167,8 +166,6 @@ walkdir(const char *path,
while (errno = 0, (de = readdir(dir)) != NULL)
{
char subpath[MAXPGPATH * 2];
- struct stat fst;
- int sret;
if (strcmp(de->d_name, ".") == 0 ||
strcmp(de->d_name, "..") == 0)
@@ -176,21 +173,20 @@ walkdir(const char *path,
snprintf(subpath, sizeof(subpath), "%s/%s", path, de->d_name);
- if (process_symlinks)
- sret = stat(subpath, &fst);
- else
- sret = lstat(subpath, &fst);
-
- if (sret < 0)
+ switch (get_dirent_type(subpath, de, process_symlinks))
{
- pg_log_error("could not stat file \"%s\": %m", subpath);
- continue;
- }
-
- if (S_ISREG(fst.st_mode))
+ case PGFILETYPE_REG:
(*action) (subpath, false);
- else if (S_ISDIR(fst.st_mode))
+ break;
+ case PGFILETYPE_DIR:
walkdir(subpath, action, false);
+ break;
+ case PGFILETYPE_ERROR:
+ pg_log_error("could not stat file \"%s\": %m", subpath);
+ break;
+ default:
+ break;
+ }
}
if (errno)
diff --git a/src/common/file_utils_febe.c b/src/common/file_utils_febe.c
new file mode 100644
index 0000000000..f4c84b8a64
--- /dev/null
+++ b/src/common/file_utils_febe.c
@@ -0,0 +1,81 @@
+/*-------------------------------------------------------------------------
+ *
+ * File-processing utility routines.
+ *
+ * Assorted utility functions to work on files, frontend and backend.
+ *
+ *
+ * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/common/file_utils_febe.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#ifdef FRONTEND
+#include "postgres_fe.h"
+#else
+#include "postgres.h"
+#endif
+
+#include <dirent.h>
+#include <sys/stat.h>
+
+#include "common/file_utils.h"
+
+/*
+ * Return the type of a directory entry.
+ */
+PGFileType
+get_dirent_type(const char *path,
+ const struct dirent *de,
+ bool look_through_symlinks)
+{
+ PGFileType result;
+
+ /*
+ * We want to know the type of a directory entry. Some systems tell us
+ * that directly in the dirent struct, but that's a BSD/GNU extension.
+ * Even when the interface is present, sometimes the type is unknown,
+ * depending on the filesystem in use or in some cases options used at
+ * filesystem creation time.
+ */
+#if defined(DT_UNKNOWN) && defined(DT_REG) && defined(DT_DIR) && defined(DT_LNK)
+ if (de->d_type == DT_REG)
+ result = PGFILETYPE_REG;
+ else if (de->d_type == DT_DIR)
+ result = PGFILETYPE_DIR;
+ else if (de->d_type == DT_LNK && !look_through_symlinks)
+ result = PGFILETYPE_LNK;
+ else
+ result = PGFILETYPE_UNKNOWN;
+#else
+ result = PGFILETYPE_UNKNOWN;
+#endif
+
+ if (result == PGFILETYPE_UNKNOWN)
+ {
+ struct stat fst;
+ int sret;
+
+
+ if (look_through_symlinks)
+ sret = stat(path, &fst);
+ else
+ sret = lstat(path, &fst);
+
+ if (sret < 0)
+ result = PGFILETYPE_ERROR;
+ else if (S_ISREG(fst.st_mode))
+ result = PGFILETYPE_REG;
+ else if (S_ISDIR(fst.st_mode))
+ result = PGFILETYPE_DIR;
+#ifdef S_ISLNK
+ else if (S_ISLNK(fst.st_mode))
+ result = PGFILETYPE_LNK;
+#endif
+ }
+
+ return result;
+}
diff --git a/src/include/common/file_utils.h b/src/include/common/file_utils.h
index a7add75efa..d723c483d2 100644
--- a/src/include/common/file_utils.h
+++ b/src/include/common/file_utils.h
@@ -1,6 +1,4 @@
/*-------------------------------------------------------------------------
- *
- * File-processing utility routines for frontend code
*
* Assorted utility functions to work on files.
*
@@ -15,10 +13,29 @@
#ifndef FILE_UTILS_H
#define FILE_UTILS_H
+#include <dirent.h>
+
+typedef enum PGFileType
+{
+ PGFILETYPE_ERROR,
+ PGFILETYPE_UNKNOWN,
+ PGFILETYPE_REG,
+ PGFILETYPE_DIR,
+ PGFILETYPE_LNK
+} PGFileType;
+
+/* Functions defined in file_utils_febe.c for both FE and BE code. */
+extern PGFileType get_dirent_type(const char *path,
+ const struct dirent *de,
+ bool look_through_symlinks);
+
+/* Functions defined in file_utils.c only for FE code. */
+#ifdef FRONTEND
extern int fsync_fname(const char *fname, bool isdir);
extern void fsync_pgdata(const char *pg_data, int serverVersion);
extern void fsync_dir_recurse(const char *dir);
extern int durable_rename(const char *oldfile, const char *newfile);
extern int fsync_parent_path(const char *fname);
+#endif
#endif /* FILE_UTILS_H */
diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm
index 20da7985c1..a64127a196 100644
--- a/src/tools/msvc/Mkvcbuild.pm
+++ b/src/tools/msvc/Mkvcbuild.pm
@@ -121,7 +121,7 @@ sub mkvcbuild
our @pgcommonallfiles = qw(
archive.c base64.c checksum_helper.c
config_info.c controldata_utils.c d2s.c encnames.c exec.c
- f2s.c file_perm.c hashfn.c ip.c jsonapi.c
+ f2s.c file_perm.c file_utils_febe.c hashfn.c ip.c jsonapi.c
keywords.c kwlookup.c link-canary.c md5.c
pg_lzcompress.c pgfnames.c psprintf.c relpath.c rmtree.c
saslprep.c scram-common.c string.c stringinfo.c unicode_norm.c username.c
--
2.20.1
[text/x-patch] v2-0002-Add-d_type-to-Win32-dirent-port.patch (2.8K, ../../CA+hUKGLwi2m7cnsYE=g+f99TAgLtWvq1eTpPVJWHHJFxVNxkGQ@mail.gmail.com/3-v2-0002-Add-d_type-to-Win32-dirent-port.patch)
download | inline diff:
From 1c4ea1bc1a23a9db1b48c8b83cfc44c376c5c811 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Thu, 3 Sep 2020 13:09:52 +1200
Subject: [PATCH v2 2/4] Add d_type to Win32 dirent port.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
The member d_type is a BSD/GNU extension to struct dirent, but Windows
has the information required to populate it so let's add it to our
emulation so that get_dirent_type() can use it.
Author: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>
Discussion: https://postgr.es/m/CA%2BhUKG%2BFzxupGGN4GpUdbzZN%2Btn6FQPHo8w0Q%2BAPH5Wz8RG%2Bww%40mail.gmail.com
---
src/include/port/win32_msvc/dirent.h | 23 +++++++++++++++++++++++
src/port/dirent.c | 15 +++++++++++++++
2 files changed, 38 insertions(+)
diff --git a/src/include/port/win32_msvc/dirent.h b/src/include/port/win32_msvc/dirent.h
index 9fabdf332b..6a54f964f5 100644
--- a/src/include/port/win32_msvc/dirent.h
+++ b/src/include/port/win32_msvc/dirent.h
@@ -10,6 +10,7 @@ struct dirent
{
long d_ino;
unsigned short d_reclen;
+ unsigned char d_type;
unsigned short d_namlen;
char d_name[MAX_PATH];
};
@@ -20,4 +21,26 @@ DIR *opendir(const char *);
struct dirent *readdir(DIR *);
int closedir(DIR *);
+/* File types for 'd_type'. */
+enum
+ {
+ DT_UNKNOWN = 0,
+# define DT_UNKNOWN DT_UNKNOWN
+ DT_FIFO = 1,
+# define DT_FIFO DT_FIFO
+ DT_CHR = 2,
+# define DT_CHR DT_CHR
+ DT_DIR = 4,
+# define DT_DIR DT_DIR
+ DT_BLK = 6,
+# define DT_BLK DT_BLK
+ DT_REG = 8,
+# define DT_REG DT_REG
+ DT_LNK = 10,
+# define DT_LNK DT_LNK
+ DT_SOCK = 12,
+# define DT_SOCK DT_SOCK
+ DT_WHT = 14
+# define DT_WHT DT_WHT
+ };
#endif
diff --git a/src/port/dirent.c b/src/port/dirent.c
index b264484fca..519dd82101 100644
--- a/src/port/dirent.c
+++ b/src/port/dirent.c
@@ -69,6 +69,7 @@ opendir(const char *dirname)
d->handle = INVALID_HANDLE_VALUE;
d->ret.d_ino = 0; /* no inodes on win32 */
d->ret.d_reclen = 0; /* not used on win32 */
+ d->ret.d_type = DT_UNKNOWN;
return d;
}
@@ -77,6 +78,7 @@ struct dirent *
readdir(DIR *d)
{
WIN32_FIND_DATA fd;
+ DWORD attrib;
if (d->handle == INVALID_HANDLE_VALUE)
{
@@ -105,6 +107,19 @@ readdir(DIR *d)
}
strcpy(d->ret.d_name, fd.cFileName); /* Both strings are MAX_PATH long */
d->ret.d_namlen = strlen(d->ret.d_name);
+ /*
+ * The only identifed types are: directory, regular file or symbolic link.
+ * Errors are treated as a file type that could not be determined.
+ */
+ attrib = GetFileAttributes(d->ret.d_name);
+ if (attrib == INVALID_FILE_ATTRIBUTES)
+ d->ret.d_type = DT_UNKNOWN;
+ else if ((attrib & FILE_ATTRIBUTE_DIRECTORY) != 0)
+ d->ret.d_type = DT_DIR;
+ else if ((attrib & FILE_ATTRIBUTE_REPARSE_POINT) != 0)
+ d->ret.d_type = DT_LNK;
+ else
+ d->ret.d_type = DT_REG;
return &d->ret;
}
--
2.20.1
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-03 05:36 Tom Lane <tgl@sss.pgh.pa.us>
parent: Thomas Munro <thomas.munro@gmail.com>
0 siblings, 1 reply; 131+ messages in thread
From: Tom Lane @ 2020-09-03 05:36 UTC (permalink / raw)
To: Thomas Munro <thomas.munro@gmail.com>; +Cc: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>; pgsql-hackers
Thomas Munro <thomas.munro@gmail.com> writes:
>> On Wed, Sep 2, 2020 at 4:35 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
>>> Hm. If we do this, I can see wanting to apply the knowledge in more
>>> places than walkdir().
> Good idea. Here's a new version that defines a new function
> get_dirent_type() in src/common/file_utils_febe.c and uses it for both
> frontend and backend walkdir().
Quick thoughts on this patch:
* The API spec for get_dirent_type() needs to say that errno is
meaningful when the return value is PGFILETYPE_ERROR. That's
something that would not be hard to break, so not documenting
the point at all doesn't seem great. More generally, I don't
especially like having the callers know that the errno is from
stat() rather than something else.
* I don't quite like the calling code you have that covers some
return values and then has a default: case without any comment.
It's not really obvious that the default: case is expected to be
hit in non-error situations, especially when there is a separate
switch path for errors. I can't find fault with the code as such,
but I think it'd be good to have a comment there. Maybe along
the lines of "Ignore file types other than regular files and
directories".
Both of these concerns would abate if we had get_dirent_type()
just throw an error itself when stat() fails, thereby removing the
PGFILETYPE_ERROR result code. I'm not 100% sold either way on
that, but it's something to think about. Is there ever going
to be a reason for the caller to ignore an error?
regards, tom lane
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-03 06:38 Thomas Munro <thomas.munro@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 131+ messages in thread
From: Thomas Munro @ 2020-09-03 06:38 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>; pgsql-hackers
On Thu, Sep 3, 2020 at 5:36 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> [request for better comments]
Ack.
> Both of these concerns would abate if we had get_dirent_type()
> just throw an error itself when stat() fails, thereby removing the
> PGFILETYPE_ERROR result code. I'm not 100% sold either way on
> that, but it's something to think about. Is there ever going
> to be a reason for the caller to ignore an error?
Hmm. Well I had it like that in an earlier version, but then I
couldn't figure out the right way to write code that would work in
both frontend and backend code, without writing two copies in two
translation units, or putting the whole thing in a header. What
approach do you prefer?
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-03 15:31 Tom Lane <tgl@sss.pgh.pa.us>
parent: Thomas Munro <thomas.munro@gmail.com>
0 siblings, 1 reply; 131+ messages in thread
From: Tom Lane @ 2020-09-03 15:31 UTC (permalink / raw)
To: Thomas Munro <thomas.munro@gmail.com>; +Cc: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>; pgsql-hackers
Thomas Munro <thomas.munro@gmail.com> writes:
> On Thu, Sep 3, 2020 at 5:36 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> Both of these concerns would abate if we had get_dirent_type()
>> just throw an error itself when stat() fails, thereby removing the
>> PGFILETYPE_ERROR result code. I'm not 100% sold either way on
>> that, but it's something to think about. Is there ever going
>> to be a reason for the caller to ignore an error?
> Hmm. Well I had it like that in an earlier version, but then I
> couldn't figure out the right way to write code that would work in
> both frontend and backend code, without writing two copies in two
> translation units, or putting the whole thing in a header. What
> approach do you prefer?
Well, there are plenty of places in src/port/ where we do things like
#ifndef FRONTEND
ereport(ERROR,
(errcode_for_file_access(),
errmsg("could not get junction for \"%s\": %s",
path, msg)));
#else
fprintf(stderr, _("could not get junction for \"%s\": %s\n"),
path, msg);
#endif
I don't see a compelling reason why this function couldn't report
stat() failures similarly, especially if we're just going to have
the callers do exactly the same thing as that anyway.
regards, tom lane
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-03 23:58 Thomas Munro <thomas.munro@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 131+ messages in thread
From: Thomas Munro @ 2020-09-03 23:58 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>; pgsql-hackers
On Fri, Sep 4, 2020 at 3:31 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Thomas Munro <thomas.munro@gmail.com> writes:
> > Hmm. Well I had it like that in an earlier version, but then I
> > couldn't figure out the right way to write code that would work in
> > both frontend and backend code, without writing two copies in two
> > translation units, or putting the whole thing in a header. What
> > approach do you prefer?
>
> Well, there are plenty of places in src/port/ where we do things like
>
> #ifndef FRONTEND
> ereport(ERROR,
> (errcode_for_file_access(),
> errmsg("could not get junction for \"%s\": %s",
> path, msg)));
> #else
> fprintf(stderr, _("could not get junction for \"%s\": %s\n"),
> path, msg);
> #endif
>
> I don't see a compelling reason why this function couldn't report
> stat() failures similarly, especially if we're just going to have
> the callers do exactly the same thing as that anyway.
Ok, so the main weird thing is that you finish up having to pass in an
elevel, but that has different meanings in FE and BE code. Note that
you still need a PGFILE_ERROR return value, because we don't log
messages at a level that exits non-locally (and that concept doesn't
even exist for FE logging).
Attachments:
[text/x-patch] v3-0001-Skip-unnecessary-stat-calls-in-walkdir.patch (9.4K, ../../CA+hUKG+o2FXKJUw7iNwQ33S1sv2mT6V6K21sB8+CRWtfeSNCew@mail.gmail.com/2-v3-0001-Skip-unnecessary-stat-calls-in-walkdir.patch)
download | inline diff:
From 6f937ccef54afdcebaa52a036591523c67ac9d41 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Thu, 3 Sep 2020 13:58:17 +1200
Subject: [PATCH v3 1/3] Skip unnecessary stat() calls in walkdir().
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Some kernels can tell us the type of a "dirent", so we can avoid a call
to stat() or lstat() in many cases. In order to be able to apply this
change to both frontend and backend versions of walkdir(), define a new
function get_dirent_type() in a new translation unit file_utils_febe.c.
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>
Discussion: https://postgr.es/m/CA%2BhUKG%2BFzxupGGN4GpUdbzZN%2Btn6FQPHo8w0Q%2BAPH5Wz8RG%2Bww%40mail.gmail.com
---
src/backend/storage/file/fd.c | 33 ++++++-----
src/common/Makefile | 1 +
src/common/file_utils.c | 32 +++++------
src/common/file_utils_febe.c | 99 ++++++++++++++++++++++++++++++++
src/include/common/file_utils.h | 22 ++++++-
src/tools/msvc/Mkvcbuild.pm | 2 +-
src/tools/pgindent/typedefs.list | 1 +
7 files changed, 154 insertions(+), 36 deletions(-)
create mode 100644 src/common/file_utils_febe.c
diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c
index f376a97ed6..bd72a87ee3 100644
--- a/src/backend/storage/file/fd.c
+++ b/src/backend/storage/file/fd.c
@@ -89,6 +89,7 @@
#include "access/xlog.h"
#include "catalog/pg_tablespace.h"
#include "common/file_perm.h"
+#include "common/file_utils.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "portability/mem.h"
@@ -3340,8 +3341,6 @@ walkdir(const char *path,
while ((de = ReadDirExtended(dir, path, elevel)) != NULL)
{
char subpath[MAXPGPATH * 2];
- struct stat fst;
- int sret;
CHECK_FOR_INTERRUPTS();
@@ -3351,23 +3350,23 @@ walkdir(const char *path,
snprintf(subpath, sizeof(subpath), "%s/%s", path, de->d_name);
- if (process_symlinks)
- sret = stat(subpath, &fst);
- else
- sret = lstat(subpath, &fst);
-
- if (sret < 0)
+ switch (get_dirent_type(subpath, de, process_symlinks, elevel))
{
- ereport(elevel,
- (errcode_for_file_access(),
- errmsg("could not stat file \"%s\": %m", subpath)));
- continue;
- }
+ case PGFILETYPE_REG:
+ (*action) (subpath, false, elevel);
+ break;
+ case PGFILETYPE_DIR:
+ walkdir(subpath, action, false, elevel);
+ break;
+ default:
- if (S_ISREG(fst.st_mode))
- (*action) (subpath, false, elevel);
- else if (S_ISDIR(fst.st_mode))
- walkdir(subpath, action, false, elevel);
+ /*
+ * Errors are already reported directly by get_dirent_type(),
+ * and any remaining symlinks and unknown file types are
+ * ignored.
+ */
+ break;
+ }
}
FreeDir(dir); /* we ignore any error here */
diff --git a/src/common/Makefile b/src/common/Makefile
index 16619e4ba8..aac92aabe1 100644
--- a/src/common/Makefile
+++ b/src/common/Makefile
@@ -56,6 +56,7 @@ OBJS_COMMON = \
exec.o \
f2s.o \
file_perm.o \
+ file_utils_febe.o \
hashfn.o \
ip.o \
jsonapi.o \
diff --git a/src/common/file_utils.c b/src/common/file_utils.c
index a2faafdf13..e24f31dd3b 100644
--- a/src/common/file_utils.c
+++ b/src/common/file_utils.c
@@ -2,7 +2,7 @@
*
* File-processing utility routines.
*
- * Assorted utility functions to work on files.
+ * Assorted utility functions to work on files, frontend only.
*
*
* Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
@@ -167,8 +167,6 @@ walkdir(const char *path,
while (errno = 0, (de = readdir(dir)) != NULL)
{
char subpath[MAXPGPATH * 2];
- struct stat fst;
- int sret;
if (strcmp(de->d_name, ".") == 0 ||
strcmp(de->d_name, "..") == 0)
@@ -176,21 +174,23 @@ walkdir(const char *path,
snprintf(subpath, sizeof(subpath), "%s/%s", path, de->d_name);
- if (process_symlinks)
- sret = stat(subpath, &fst);
- else
- sret = lstat(subpath, &fst);
-
- if (sret < 0)
+ switch (get_dirent_type(subpath, de, process_symlinks, PG_LOG_ERROR))
{
- pg_log_error("could not stat file \"%s\": %m", subpath);
- continue;
+ case PGFILETYPE_REG:
+ (*action) (subpath, false);
+ break;
+ case PGFILETYPE_DIR:
+ walkdir(subpath, action, false);
+ break;
+ default:
+
+ /*
+ * Errors are already reported directly by get_dirent_type(),
+ * and any remaining symlinks and unknown file types are
+ * ignored.
+ */
+ break;
}
-
- if (S_ISREG(fst.st_mode))
- (*action) (subpath, false);
- else if (S_ISDIR(fst.st_mode))
- walkdir(subpath, action, false);
}
if (errno)
diff --git a/src/common/file_utils_febe.c b/src/common/file_utils_febe.c
new file mode 100644
index 0000000000..3c00ab7c4d
--- /dev/null
+++ b/src/common/file_utils_febe.c
@@ -0,0 +1,99 @@
+/*-------------------------------------------------------------------------
+ *
+ * File-processing utility routines.
+ *
+ * Assorted utility functions to work on files, frontend and backend.
+ *
+ *
+ * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/common/file_utils_febe.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#ifdef FRONTEND
+#include "postgres_fe.h"
+#else
+#include "postgres.h"
+#endif
+
+#include <dirent.h>
+#include <sys/stat.h>
+
+#include "common/file_utils.h"
+#ifdef FRONTEND
+#include "common/logging.h"
+#else
+#include "utils/elog.h"
+#endif
+
+/*
+ * Return the type of a directory entry.
+ *
+ * In frontend code, elevel should be a level from logging.h; in backend code
+ * it should be an error level from elog.h.
+ */
+PGFileType
+get_dirent_type(const char *path,
+ const struct dirent *de,
+ bool look_through_symlinks,
+ int elevel)
+{
+ PGFileType result;
+
+ /*
+ * We want to know the type of a directory entry. Some systems tell us
+ * that directly in the dirent struct, but that's a BSD/GNU extension.
+ * Even when the interface is present, sometimes the type is unknown,
+ * depending on the filesystem in use or in some cases options used at
+ * filesystem creation time.
+ */
+#if defined(DT_UNKNOWN) && defined(DT_REG) && defined(DT_DIR) && defined(DT_LNK)
+ if (de->d_type == DT_REG)
+ result = PGFILETYPE_REG;
+ else if (de->d_type == DT_DIR)
+ result = PGFILETYPE_DIR;
+ else if (de->d_type == DT_LNK && !look_through_symlinks)
+ result = PGFILETYPE_LNK;
+ else
+ result = PGFILETYPE_UNKNOWN;
+#else
+ result = PGFILETYPE_UNKNOWN;
+#endif
+
+ if (result == PGFILETYPE_UNKNOWN)
+ {
+ struct stat fst;
+ int sret;
+
+
+ if (look_through_symlinks)
+ sret = stat(path, &fst);
+ else
+ sret = lstat(path, &fst);
+
+ if (sret < 0)
+ {
+ result = PGFILETYPE_ERROR;
+#ifdef FRONTEND
+ pg_log_generic(elevel, "could not stat file \"%s\": %m", path);
+#else
+ ereport(elevel,
+ (errcode_for_file_access(),
+ errmsg("could not stat file \"%s\": %m", path)));
+#endif
+ }
+ else if (S_ISREG(fst.st_mode))
+ result = PGFILETYPE_REG;
+ else if (S_ISDIR(fst.st_mode))
+ result = PGFILETYPE_DIR;
+#ifdef S_ISLNK
+ else if (S_ISLNK(fst.st_mode))
+ result = PGFILETYPE_LNK;
+#endif
+ }
+
+ return result;
+}
diff --git a/src/include/common/file_utils.h b/src/include/common/file_utils.h
index a7add75efa..16c7e7e249 100644
--- a/src/include/common/file_utils.h
+++ b/src/include/common/file_utils.h
@@ -1,6 +1,4 @@
/*-------------------------------------------------------------------------
- *
- * File-processing utility routines for frontend code
*
* Assorted utility functions to work on files.
*
@@ -15,10 +13,30 @@
#ifndef FILE_UTILS_H
#define FILE_UTILS_H
+#include <dirent.h>
+
+typedef enum PGFileType
+{
+ PGFILETYPE_ERROR,
+ PGFILETYPE_UNKNOWN,
+ PGFILETYPE_REG,
+ PGFILETYPE_DIR,
+ PGFILETYPE_LNK
+} PGFileType;
+
+/* Functions defined in file_utils_febe.c for both FE and BE code. */
+extern PGFileType get_dirent_type(const char *path,
+ const struct dirent *de,
+ bool look_through_symlinks,
+ int elevel);
+
+/* Functions defined in file_utils.c only for FE code. */
+#ifdef FRONTEND
extern int fsync_fname(const char *fname, bool isdir);
extern void fsync_pgdata(const char *pg_data, int serverVersion);
extern void fsync_dir_recurse(const char *dir);
extern int durable_rename(const char *oldfile, const char *newfile);
extern int fsync_parent_path(const char *fname);
+#endif
#endif /* FILE_UTILS_H */
diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm
index 20da7985c1..a64127a196 100644
--- a/src/tools/msvc/Mkvcbuild.pm
+++ b/src/tools/msvc/Mkvcbuild.pm
@@ -121,7 +121,7 @@ sub mkvcbuild
our @pgcommonallfiles = qw(
archive.c base64.c checksum_helper.c
config_info.c controldata_utils.c d2s.c encnames.c exec.c
- f2s.c file_perm.c hashfn.c ip.c jsonapi.c
+ f2s.c file_perm.c file_utils_febe.c hashfn.c ip.c jsonapi.c
keywords.c kwlookup.c link-canary.c md5.c
pg_lzcompress.c pgfnames.c psprintf.c relpath.c rmtree.c
saslprep.c scram-common.c string.c stringinfo.c unicode_norm.c username.c
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 3d990463ce..b4d40dda16 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -1514,6 +1514,7 @@ PGEventResultCopy
PGEventResultCreate
PGEventResultDestroy
PGFInfoFunction
+PGFileType
PGFunction
PGLZ_HistEntry
PGLZ_Strategy
--
2.20.1
[text/x-patch] v3-0002-Add-d_type-to-Win32-dirent-port.patch (2.8K, ../../CA+hUKG+o2FXKJUw7iNwQ33S1sv2mT6V6K21sB8+CRWtfeSNCew@mail.gmail.com/3-v3-0002-Add-d_type-to-Win32-dirent-port.patch)
download | inline diff:
From 616eba62a637b9b7440680d9ca4490d36347be46 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Thu, 3 Sep 2020 13:09:52 +1200
Subject: [PATCH v3 2/3] Add d_type to Win32 dirent port.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
The member d_type is a BSD/GNU extension to struct dirent, but Windows
has the information required to populate it so let's add it to our
emulation so that get_dirent_type() can use it.
Author: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>
Discussion: https://postgr.es/m/CA%2BhUKG%2BFzxupGGN4GpUdbzZN%2Btn6FQPHo8w0Q%2BAPH5Wz8RG%2Bww%40mail.gmail.com
---
src/include/port/win32_msvc/dirent.h | 23 +++++++++++++++++++++++
src/port/dirent.c | 15 +++++++++++++++
2 files changed, 38 insertions(+)
diff --git a/src/include/port/win32_msvc/dirent.h b/src/include/port/win32_msvc/dirent.h
index 9fabdf332b..6a54f964f5 100644
--- a/src/include/port/win32_msvc/dirent.h
+++ b/src/include/port/win32_msvc/dirent.h
@@ -10,6 +10,7 @@ struct dirent
{
long d_ino;
unsigned short d_reclen;
+ unsigned char d_type;
unsigned short d_namlen;
char d_name[MAX_PATH];
};
@@ -20,4 +21,26 @@ DIR *opendir(const char *);
struct dirent *readdir(DIR *);
int closedir(DIR *);
+/* File types for 'd_type'. */
+enum
+ {
+ DT_UNKNOWN = 0,
+# define DT_UNKNOWN DT_UNKNOWN
+ DT_FIFO = 1,
+# define DT_FIFO DT_FIFO
+ DT_CHR = 2,
+# define DT_CHR DT_CHR
+ DT_DIR = 4,
+# define DT_DIR DT_DIR
+ DT_BLK = 6,
+# define DT_BLK DT_BLK
+ DT_REG = 8,
+# define DT_REG DT_REG
+ DT_LNK = 10,
+# define DT_LNK DT_LNK
+ DT_SOCK = 12,
+# define DT_SOCK DT_SOCK
+ DT_WHT = 14
+# define DT_WHT DT_WHT
+ };
#endif
diff --git a/src/port/dirent.c b/src/port/dirent.c
index b264484fca..519dd82101 100644
--- a/src/port/dirent.c
+++ b/src/port/dirent.c
@@ -69,6 +69,7 @@ opendir(const char *dirname)
d->handle = INVALID_HANDLE_VALUE;
d->ret.d_ino = 0; /* no inodes on win32 */
d->ret.d_reclen = 0; /* not used on win32 */
+ d->ret.d_type = DT_UNKNOWN;
return d;
}
@@ -77,6 +78,7 @@ struct dirent *
readdir(DIR *d)
{
WIN32_FIND_DATA fd;
+ DWORD attrib;
if (d->handle == INVALID_HANDLE_VALUE)
{
@@ -105,6 +107,19 @@ readdir(DIR *d)
}
strcpy(d->ret.d_name, fd.cFileName); /* Both strings are MAX_PATH long */
d->ret.d_namlen = strlen(d->ret.d_name);
+ /*
+ * The only identifed types are: directory, regular file or symbolic link.
+ * Errors are treated as a file type that could not be determined.
+ */
+ attrib = GetFileAttributes(d->ret.d_name);
+ if (attrib == INVALID_FILE_ATTRIBUTES)
+ d->ret.d_type = DT_UNKNOWN;
+ else if ((attrib & FILE_ATTRIBUTE_DIRECTORY) != 0)
+ d->ret.d_type = DT_DIR;
+ else if ((attrib & FILE_ATTRIBUTE_REPARSE_POINT) != 0)
+ d->ret.d_type = DT_LNK;
+ else
+ d->ret.d_type = DT_REG;
return &d->ret;
}
--
2.20.1
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-04 19:37 Alvaro Herrera <alvherre@2ndquadrant.com>
parent: Thomas Munro <thomas.munro@gmail.com>
0 siblings, 1 reply; 131+ messages in thread
From: Alvaro Herrera @ 2020-09-04 19:37 UTC (permalink / raw)
To: Thomas Munro <thomas.munro@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>; pgsql-hackers
On 2020-Sep-04, Thomas Munro wrote:
> @@ -10,6 +10,7 @@ struct dirent
> {
> long d_ino;
> unsigned short d_reclen;
> + unsigned char d_type;
> unsigned short d_namlen;
> char d_name[MAX_PATH];
> };
> @@ -20,4 +21,26 @@ DIR *opendir(const char *);
> struct dirent *readdir(DIR *);
> int closedir(DIR *);
>
> +/* File types for 'd_type'. */
> +enum
> + {
> + DT_UNKNOWN = 0,
> +# define DT_UNKNOWN DT_UNKNOWN
Uhm ... what do these #defines do? They look a bit funny.
Would it make sense to give this enum a name, and then use that name in
struct dirent's definition, instead of unsigned char?
--
Álvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-04 20:03 Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>
parent: Alvaro Herrera <alvherre@2ndquadrant.com>
0 siblings, 1 reply; 131+ messages in thread
From: Juan José Santamaría Flecha @ 2020-09-04 20:03 UTC (permalink / raw)
To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Thomas Munro <thomas.munro@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; pgsql-hackers
On Fri, Sep 4, 2020 at 9:37 PM Alvaro Herrera <alvherre@2ndquadrant.com>
wrote:
> On 2020-Sep-04, Thomas Munro wrote:
>
> > @@ -10,6 +10,7 @@ struct dirent
> > {
> > long d_ino;
> > unsigned short d_reclen;
> > + unsigned char d_type;
> > unsigned short d_namlen;
> > char d_name[MAX_PATH];
> > };
> > @@ -20,4 +21,26 @@ DIR *opendir(const char *);
> > struct dirent *readdir(DIR *);
> > int closedir(DIR *);
> >
> > +/* File types for 'd_type'. */
> > +enum
> > + {
> > + DT_UNKNOWN = 0,
> > +# define DT_UNKNOWN DT_UNKNOWN
>
> Uhm ... what do these #defines do? They look a bit funny.
>
> Would it make sense to give this enum a name, and then use that name in
> struct dirent's definition, instead of unsigned char?
>
They mimic POSIX dirent.h. I would rather stick to that.
Regards,
Juan José Santamaría Flecha
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-04 20:28 Alvaro Herrera <alvherre@2ndquadrant.com>
parent: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>
0 siblings, 1 reply; 131+ messages in thread
From: Alvaro Herrera @ 2020-09-04 20:28 UTC (permalink / raw)
To: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>; +Cc: Thomas Munro <thomas.munro@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; pgsql-hackers
On 2020-Sep-04, Juan José Santamaría Flecha wrote:
> On Fri, Sep 4, 2020 at 9:37 PM Alvaro Herrera <alvherre@2ndquadrant.com>
> wrote:
>
> > On 2020-Sep-04, Thomas Munro wrote:
> > >
> > > +/* File types for 'd_type'. */
> > > +enum
> > > + {
> > > + DT_UNKNOWN = 0,
> > > +# define DT_UNKNOWN DT_UNKNOWN
> >
> > Uhm ... what do these #defines do? They look a bit funny.
> >
> > Would it make sense to give this enum a name, and then use that name in
> > struct dirent's definition, instead of unsigned char?
>
> They mimic POSIX dirent.h. I would rather stick to that.
Ah ... they do?
If you remove the #define lines, what happens to your patch?
--
Álvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-04 20:34 Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>
parent: Alvaro Herrera <alvherre@2ndquadrant.com>
0 siblings, 1 reply; 131+ messages in thread
From: Juan José Santamaría Flecha @ 2020-09-04 20:34 UTC (permalink / raw)
To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Thomas Munro <thomas.munro@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; pgsql-hackers
On Fri, Sep 4, 2020 at 10:28 PM Alvaro Herrera <alvherre@2ndquadrant.com>
wrote:
> On 2020-Sep-04, Juan José Santamaría Flecha wrote:
>
> > On Fri, Sep 4, 2020 at 9:37 PM Alvaro Herrera <alvherre@2ndquadrant.com>
> > wrote:
> >
> > > On 2020-Sep-04, Thomas Munro wrote:
>
> > > >
> > > > +/* File types for 'd_type'. */
> > > > +enum
> > > > + {
> > > > + DT_UNKNOWN = 0,
> > > > +# define DT_UNKNOWN DT_UNKNOWN
> > >
> > > Uhm ... what do these #defines do? They look a bit funny.
> > >
> > > Would it make sense to give this enum a name, and then use that name in
> > > struct dirent's definition, instead of unsigned char?
> >
> > They mimic POSIX dirent.h. I would rather stick to that.
>
> Ah ... they do?
>
> If you remove the #define lines, what happens to your patch?
>
If will fail to detect that the patch makes the optimisation available for
WIN32:
+#if defined(DT_UNKNOWN) && defined(DT_REG) && defined(DT_DIR) &&
defined(DT_LNK)
Regards,
Juan José Santamaría Flecha
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-04 20:43 Alvaro Herrera <alvherre@2ndquadrant.com>
parent: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>
0 siblings, 1 reply; 131+ messages in thread
From: Alvaro Herrera @ 2020-09-04 20:43 UTC (permalink / raw)
To: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>; +Cc: Thomas Munro <thomas.munro@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; pgsql-hackers
On 2020-Sep-04, Juan José Santamaría Flecha wrote:
> If will fail to detect that the patch makes the optimisation available for
> WIN32:
>
> +#if defined(DT_UNKNOWN) && defined(DT_REG) && defined(DT_DIR) &&
> defined(DT_LNK)
Oh, I see. I suggest that it'd be better to change this line instead.
--
Álvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-04 21:39 Tom Lane <tgl@sss.pgh.pa.us>
parent: Alvaro Herrera <alvherre@2ndquadrant.com>
0 siblings, 1 reply; 131+ messages in thread
From: Tom Lane @ 2020-09-04 21:39 UTC (permalink / raw)
To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers
Alvaro Herrera <alvherre@2ndquadrant.com> writes:
> On 2020-Sep-04, Juan José Santamaría Flecha wrote:
>> If will fail to detect that the patch makes the optimisation available for
>> WIN32:
>>
>> +#if defined(DT_UNKNOWN) && defined(DT_REG) && defined(DT_DIR) &&
>> defined(DT_LNK)
> Oh, I see. I suggest that it'd be better to change this line instead.
I think that it's standard to test for such symbols by seeing
if they're defined as macros ... not least because that's the *only*
way to test their existence in C.
Personally, what I'd do is lose the enum and just define the macros
with simple integer constant values.
regards, tom lane
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-04 21:45 Andres Freund <andres@anarazel.de>
parent: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>
1 sibling, 1 reply; 131+ messages in thread
From: Andres Freund @ 2020-09-04 21:45 UTC (permalink / raw)
To: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers
Hi,
On 2020-09-02 17:51:27 +0200, Juan José Santamaría Flecha wrote:
> Win32 could also benefit from this micro-optimisation if we expanded the
> dirent port to include d_type. Please find attached a patch that does
> so
.
> }
> strcpy(d->ret.d_name, fd.cFileName); /* Both strings are MAX_PATH long */
> d->ret.d_namlen = strlen(d->ret.d_name);
> + /*
> + * The only identifed types are: directory, regular file or symbolic link.
> + * Errors are treated as a file type that could not be determined.
> + */
> + attrib = GetFileAttributes(d->ret.d_name);
> + if (attrib == INVALID_FILE_ATTRIBUTES)
> + d->ret.d_type = DT_UNKNOWN;
> + else if ((attrib & FILE_ATTRIBUTE_DIRECTORY) != 0)
> + d->ret.d_type = DT_DIR;
> + else if ((attrib & FILE_ATTRIBUTE_REPARSE_POINT) != 0)
> + d->ret.d_type = DT_LNK;
> + else
> + d->ret.d_type = DT_REG;
>
> return &d->ret;
> }
Is this really an optimization? The benefit of Thomas' patch is that
that information sometimes already is there. But here you're doing a
separate lookup with GetFileAttributes()?
What am I missing?
Greetings,
Andres Freund
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-04 21:52 Alvaro Herrera <alvherre@2ndquadrant.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 131+ messages in thread
From: Alvaro Herrera @ 2020-09-04 21:52 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>; Thomas Munro <thomas.munro@gmail.com>; pgsql-hackers
On 2020-Sep-04, Tom Lane wrote:
> I think that it's standard to test for such symbols by seeing
> if they're defined as macros ... not least because that's the *only*
> way to test their existence in C.
I guess since what we're doing is emulating standard readdir(), that
makes sense.
> Personally, what I'd do is lose the enum and just define the macros
> with simple integer constant values.
WFM.
--
Álvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-04 23:15 Thomas Munro <thomas.munro@gmail.com>
parent: Andres Freund <andres@anarazel.de>
0 siblings, 1 reply; 131+ messages in thread
From: Thomas Munro @ 2020-09-04 23:15 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; pgsql-hackers
On Sat, Sep 5, 2020 at 9:45 AM Andres Freund <andres@anarazel.de> wrote:
> On 2020-09-02 17:51:27 +0200, Juan José Santamaría Flecha wrote:
> > + attrib = GetFileAttributes(d->ret.d_name);
>
> Is this really an optimization? The benefit of Thomas' patch is that
> that information sometimes already is there. But here you're doing a
> separate lookup with GetFileAttributes()?
Well as discussed already, our stat() emulation on Windows does
multiple syscalls, so it's a slight improvement. However, it looks
like we might be missing a further opportunity here... Doesn't
Windows already give us the flags we need in the dwFileAttributes
member of the WIN32_FIND_DATA object that the Find{First,Next}File()
functions populate?
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-05 00:13 Andres Freund <andres@anarazel.de>
parent: Thomas Munro <thomas.munro@gmail.com>
0 siblings, 1 reply; 131+ messages in thread
From: Andres Freund @ 2020-09-05 00:13 UTC (permalink / raw)
To: Thomas Munro <thomas.munro@gmail.com>; +Cc: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; pgsql-hackers
Hi,
On 2020-09-05 11:15:07 +1200, Thomas Munro wrote:
> On Sat, Sep 5, 2020 at 9:45 AM Andres Freund <andres@anarazel.de> wrote:
> > On 2020-09-02 17:51:27 +0200, Juan José Santamaría Flecha wrote:
> > > + attrib = GetFileAttributes(d->ret.d_name);
> >
> > Is this really an optimization? The benefit of Thomas' patch is that
> > that information sometimes already is there. But here you're doing a
> > separate lookup with GetFileAttributes()?
>
> Well as discussed already, our stat() emulation on Windows does
> multiple syscalls, so it's a slight improvement.
But the patch is patching readdir(), not just walkdir(). Not all
readdir() / ReadDir() callers necessarily do a stat() and many continue
to do a stat() after the patches. So for all of those except walkdir(),
some like RemoveOldXlogFiles() sometimes being noticable cost wise, the
patch will increase the cost on windows. No? That's quite different
from utilizing "free" information.
> However, it looks like we might be missing a further opportunity
> here... Doesn't Windows already give us the flags we need in the
> dwFileAttributes member of the WIN32_FIND_DATA object that the
> Find{First,Next}File() functions populate?
That'd be better...
Greetings,
Andres Freund
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-05 17:22 Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>
parent: Andres Freund <andres@anarazel.de>
0 siblings, 1 reply; 131+ messages in thread
From: Juan José Santamaría Flecha @ 2020-09-05 17:22 UTC (permalink / raw)
To: Andres Freund <andres@anarazel.de>; +Cc: Thomas Munro <thomas.munro@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; pgsql-hackers
On Sat, Sep 5, 2020 at 2:13 AM Andres Freund <andres@anarazel.de> wrote:
>
> > However, it looks like we might be missing a further opportunity
> > here... Doesn't Windows already give us the flags we need in the
> > dwFileAttributes member of the WIN32_FIND_DATA object that the
> > Find{First,Next}File() functions populate?
>
> That'd be better...
>
At first I did not see how to get DT_LNK directly, but it is possible
without additional calls, so please find attached a version with that logic.
This version also drops the enum, defining just the macros.
Regards,
Juan José Santamaría Flecha
Attachments:
[application/octet-stream] v4-0001-Skip-unnecessary-stat-calls-in-walkdir.patch (9.4K, ../../CAC+AXB12ukw-D22CcYC4F+=pRqgvcLgh5x2Wa07UaUaD+Q0vWA@mail.gmail.com/3-v4-0001-Skip-unnecessary-stat-calls-in-walkdir.patch)
download | inline diff:
From 6f937ccef54afdcebaa52a036591523c67ac9d41 Mon Sep 17 00:00:00 2001
From: Thomas Munro <thomas.munro@gmail.com>
Date: Thu, 3 Sep 2020 13:58:17 +1200
Subject: [PATCH v3 1/3] Skip unnecessary stat() calls in walkdir().
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Some kernels can tell us the type of a "dirent", so we can avoid a call
to stat() or lstat() in many cases. In order to be able to apply this
change to both frontend and backend versions of walkdir(), define a new
function get_dirent_type() in a new translation unit file_utils_febe.c.
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>
Discussion: https://postgr.es/m/CA%2BhUKG%2BFzxupGGN4GpUdbzZN%2Btn6FQPHo8w0Q%2BAPH5Wz8RG%2Bww%40mail.gmail.com
---
src/backend/storage/file/fd.c | 33 ++++++-----
src/common/Makefile | 1 +
src/common/file_utils.c | 32 +++++------
src/common/file_utils_febe.c | 99 ++++++++++++++++++++++++++++++++
src/include/common/file_utils.h | 22 ++++++-
src/tools/msvc/Mkvcbuild.pm | 2 +-
src/tools/pgindent/typedefs.list | 1 +
7 files changed, 154 insertions(+), 36 deletions(-)
create mode 100644 src/common/file_utils_febe.c
diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c
index f376a97ed6..bd72a87ee3 100644
--- a/src/backend/storage/file/fd.c
+++ b/src/backend/storage/file/fd.c
@@ -89,6 +89,7 @@
#include "access/xlog.h"
#include "catalog/pg_tablespace.h"
#include "common/file_perm.h"
+#include "common/file_utils.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "portability/mem.h"
@@ -3340,8 +3341,6 @@ walkdir(const char *path,
while ((de = ReadDirExtended(dir, path, elevel)) != NULL)
{
char subpath[MAXPGPATH * 2];
- struct stat fst;
- int sret;
CHECK_FOR_INTERRUPTS();
@@ -3351,23 +3350,23 @@ walkdir(const char *path,
snprintf(subpath, sizeof(subpath), "%s/%s", path, de->d_name);
- if (process_symlinks)
- sret = stat(subpath, &fst);
- else
- sret = lstat(subpath, &fst);
-
- if (sret < 0)
+ switch (get_dirent_type(subpath, de, process_symlinks, elevel))
{
- ereport(elevel,
- (errcode_for_file_access(),
- errmsg("could not stat file \"%s\": %m", subpath)));
- continue;
- }
+ case PGFILETYPE_REG:
+ (*action) (subpath, false, elevel);
+ break;
+ case PGFILETYPE_DIR:
+ walkdir(subpath, action, false, elevel);
+ break;
+ default:
- if (S_ISREG(fst.st_mode))
- (*action) (subpath, false, elevel);
- else if (S_ISDIR(fst.st_mode))
- walkdir(subpath, action, false, elevel);
+ /*
+ * Errors are already reported directly by get_dirent_type(),
+ * and any remaining symlinks and unknown file types are
+ * ignored.
+ */
+ break;
+ }
}
FreeDir(dir); /* we ignore any error here */
diff --git a/src/common/Makefile b/src/common/Makefile
index 16619e4ba8..aac92aabe1 100644
--- a/src/common/Makefile
+++ b/src/common/Makefile
@@ -56,6 +56,7 @@ OBJS_COMMON = \
exec.o \
f2s.o \
file_perm.o \
+ file_utils_febe.o \
hashfn.o \
ip.o \
jsonapi.o \
diff --git a/src/common/file_utils.c b/src/common/file_utils.c
index a2faafdf13..e24f31dd3b 100644
--- a/src/common/file_utils.c
+++ b/src/common/file_utils.c
@@ -2,7 +2,7 @@
*
* File-processing utility routines.
*
- * Assorted utility functions to work on files.
+ * Assorted utility functions to work on files, frontend only.
*
*
* Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
@@ -167,8 +167,6 @@ walkdir(const char *path,
while (errno = 0, (de = readdir(dir)) != NULL)
{
char subpath[MAXPGPATH * 2];
- struct stat fst;
- int sret;
if (strcmp(de->d_name, ".") == 0 ||
strcmp(de->d_name, "..") == 0)
@@ -176,21 +174,23 @@ walkdir(const char *path,
snprintf(subpath, sizeof(subpath), "%s/%s", path, de->d_name);
- if (process_symlinks)
- sret = stat(subpath, &fst);
- else
- sret = lstat(subpath, &fst);
-
- if (sret < 0)
+ switch (get_dirent_type(subpath, de, process_symlinks, PG_LOG_ERROR))
{
- pg_log_error("could not stat file \"%s\": %m", subpath);
- continue;
+ case PGFILETYPE_REG:
+ (*action) (subpath, false);
+ break;
+ case PGFILETYPE_DIR:
+ walkdir(subpath, action, false);
+ break;
+ default:
+
+ /*
+ * Errors are already reported directly by get_dirent_type(),
+ * and any remaining symlinks and unknown file types are
+ * ignored.
+ */
+ break;
}
-
- if (S_ISREG(fst.st_mode))
- (*action) (subpath, false);
- else if (S_ISDIR(fst.st_mode))
- walkdir(subpath, action, false);
}
if (errno)
diff --git a/src/common/file_utils_febe.c b/src/common/file_utils_febe.c
new file mode 100644
index 0000000000..3c00ab7c4d
--- /dev/null
+++ b/src/common/file_utils_febe.c
@@ -0,0 +1,99 @@
+/*-------------------------------------------------------------------------
+ *
+ * File-processing utility routines.
+ *
+ * Assorted utility functions to work on files, frontend and backend.
+ *
+ *
+ * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/common/file_utils_febe.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#ifdef FRONTEND
+#include "postgres_fe.h"
+#else
+#include "postgres.h"
+#endif
+
+#include <dirent.h>
+#include <sys/stat.h>
+
+#include "common/file_utils.h"
+#ifdef FRONTEND
+#include "common/logging.h"
+#else
+#include "utils/elog.h"
+#endif
+
+/*
+ * Return the type of a directory entry.
+ *
+ * In frontend code, elevel should be a level from logging.h; in backend code
+ * it should be an error level from elog.h.
+ */
+PGFileType
+get_dirent_type(const char *path,
+ const struct dirent *de,
+ bool look_through_symlinks,
+ int elevel)
+{
+ PGFileType result;
+
+ /*
+ * We want to know the type of a directory entry. Some systems tell us
+ * that directly in the dirent struct, but that's a BSD/GNU extension.
+ * Even when the interface is present, sometimes the type is unknown,
+ * depending on the filesystem in use or in some cases options used at
+ * filesystem creation time.
+ */
+#if defined(DT_UNKNOWN) && defined(DT_REG) && defined(DT_DIR) && defined(DT_LNK)
+ if (de->d_type == DT_REG)
+ result = PGFILETYPE_REG;
+ else if (de->d_type == DT_DIR)
+ result = PGFILETYPE_DIR;
+ else if (de->d_type == DT_LNK && !look_through_symlinks)
+ result = PGFILETYPE_LNK;
+ else
+ result = PGFILETYPE_UNKNOWN;
+#else
+ result = PGFILETYPE_UNKNOWN;
+#endif
+
+ if (result == PGFILETYPE_UNKNOWN)
+ {
+ struct stat fst;
+ int sret;
+
+
+ if (look_through_symlinks)
+ sret = stat(path, &fst);
+ else
+ sret = lstat(path, &fst);
+
+ if (sret < 0)
+ {
+ result = PGFILETYPE_ERROR;
+#ifdef FRONTEND
+ pg_log_generic(elevel, "could not stat file \"%s\": %m", path);
+#else
+ ereport(elevel,
+ (errcode_for_file_access(),
+ errmsg("could not stat file \"%s\": %m", path)));
+#endif
+ }
+ else if (S_ISREG(fst.st_mode))
+ result = PGFILETYPE_REG;
+ else if (S_ISDIR(fst.st_mode))
+ result = PGFILETYPE_DIR;
+#ifdef S_ISLNK
+ else if (S_ISLNK(fst.st_mode))
+ result = PGFILETYPE_LNK;
+#endif
+ }
+
+ return result;
+}
diff --git a/src/include/common/file_utils.h b/src/include/common/file_utils.h
index a7add75efa..16c7e7e249 100644
--- a/src/include/common/file_utils.h
+++ b/src/include/common/file_utils.h
@@ -1,6 +1,4 @@
/*-------------------------------------------------------------------------
- *
- * File-processing utility routines for frontend code
*
* Assorted utility functions to work on files.
*
@@ -15,10 +13,30 @@
#ifndef FILE_UTILS_H
#define FILE_UTILS_H
+#include <dirent.h>
+
+typedef enum PGFileType
+{
+ PGFILETYPE_ERROR,
+ PGFILETYPE_UNKNOWN,
+ PGFILETYPE_REG,
+ PGFILETYPE_DIR,
+ PGFILETYPE_LNK
+} PGFileType;
+
+/* Functions defined in file_utils_febe.c for both FE and BE code. */
+extern PGFileType get_dirent_type(const char *path,
+ const struct dirent *de,
+ bool look_through_symlinks,
+ int elevel);
+
+/* Functions defined in file_utils.c only for FE code. */
+#ifdef FRONTEND
extern int fsync_fname(const char *fname, bool isdir);
extern void fsync_pgdata(const char *pg_data, int serverVersion);
extern void fsync_dir_recurse(const char *dir);
extern int durable_rename(const char *oldfile, const char *newfile);
extern int fsync_parent_path(const char *fname);
+#endif
#endif /* FILE_UTILS_H */
diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm
index 20da7985c1..a64127a196 100644
--- a/src/tools/msvc/Mkvcbuild.pm
+++ b/src/tools/msvc/Mkvcbuild.pm
@@ -121,7 +121,7 @@ sub mkvcbuild
our @pgcommonallfiles = qw(
archive.c base64.c checksum_helper.c
config_info.c controldata_utils.c d2s.c encnames.c exec.c
- f2s.c file_perm.c hashfn.c ip.c jsonapi.c
+ f2s.c file_perm.c file_utils_febe.c hashfn.c ip.c jsonapi.c
keywords.c kwlookup.c link-canary.c md5.c
pg_lzcompress.c pgfnames.c psprintf.c relpath.c rmtree.c
saslprep.c scram-common.c string.c stringinfo.c unicode_norm.c username.c
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 3d990463ce..b4d40dda16 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -1514,6 +1514,7 @@ PGEventResultCopy
PGEventResultCreate
PGEventResultDestroy
PGFInfoFunction
+PGFileType
PGFunction
PGLZ_HistEntry
PGLZ_Strategy
--
2.20.1
[application/octet-stream] v4-0002-Add-d_type-to-WIN32-dirent-port.patch (1.6K, ../../CAC+AXB12ukw-D22CcYC4F+=pRqgvcLgh5x2Wa07UaUaD+Q0vWA@mail.gmail.com/4-v4-0002-Add-d_type-to-WIN32-dirent-port.patch)
download | inline diff:
diff --git a/src/include/port/win32_msvc/dirent.h b/src/include/port/win32_msvc/dirent.h
index 9fabdf3..d339242 100644
--- a/src/include/port/win32_msvc/dirent.h
+++ b/src/include/port/win32_msvc/dirent.h
@@ -10,6 +10,7 @@ struct dirent
{
long d_ino;
unsigned short d_reclen;
+ unsigned char d_type;
unsigned short d_namlen;
char d_name[MAX_PATH];
};
@@ -20,4 +21,14 @@ DIR *opendir(const char *);
struct dirent *readdir(DIR *);
int closedir(DIR *);
+/* File types for 'd_type'. */
+# define DT_UNKNOWN 0
+# define DT_FIFO 1
+# define DT_CHR 2
+# define DT_DIR 4
+# define DT_BLK 6
+# define DT_REG 8
+# define DT_LNK 10
+# define DT_SOCK 12
+# define DT_WHT 14
#endif
diff --git a/src/port/dirent.c b/src/port/dirent.c
index b264484..83db153 100644
--- a/src/port/dirent.c
+++ b/src/port/dirent.c
@@ -69,6 +69,7 @@ opendir(const char *dirname)
d->handle = INVALID_HANDLE_VALUE;
d->ret.d_ino = 0; /* no inodes on win32 */
d->ret.d_reclen = 0; /* not used on win32 */
+ d->ret.d_type = DT_UNKNOWN;
return d;
}
@@ -105,6 +106,15 @@ readdir(DIR *d)
}
strcpy(d->ret.d_name, fd.cFileName); /* Both strings are MAX_PATH long */
d->ret.d_namlen = strlen(d->ret.d_name);
+ /* The only identifed types are: directory, regular file or symbolic link */
+ if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
+ d->ret.d_type = DT_DIR;
+ /* For reparse points dwReserved0 field will contain the ReparseTag */
+ else if ((fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0
+ && (fd.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT))
+ d->ret.d_type = DT_LNK;
+ else
+ d->ret.d_type = DT_REG;
return &d->ret;
}
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-06 22:26 Thomas Munro <thomas.munro@gmail.com>
parent: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>
0 siblings, 2 replies; 131+ messages in thread
From: Thomas Munro @ 2020-09-06 22:26 UTC (permalink / raw)
To: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>; +Cc: Andres Freund <andres@anarazel.de>; Tom Lane <tgl@sss.pgh.pa.us>; pgsql-hackers
On Sun, Sep 6, 2020 at 5:23 AM Juan José Santamaría Flecha
<juanjo.santamaria@gmail.com> wrote:
> On Sat, Sep 5, 2020 at 2:13 AM Andres Freund <andres@anarazel.de> wrote:
>> > However, it looks like we might be missing a further opportunity
>> > here... Doesn't Windows already give us the flags we need in the
>> > dwFileAttributes member of the WIN32_FIND_DATA object that the
>> > Find{First,Next}File() functions populate?
>>
>> That'd be better...
>
>
> At first I did not see how to get DT_LNK directly, but it is possible without additional calls, so please find attached a version with that logic.
>
> This version also drops the enum, defining just the macros.
Excellent. I'd like to commit these soon, unless someone has a better
idea for how to name file_utils_febe.c.
I think the following is a little mysterious, but it does seem to be
what people do for this in other projects. It is the documented way
to detect mount points, and I guess IO_REPARSE_TAG_MOUNT_POINT is
either overloaded also for junctions, or junctions are the same thing
as mount points. It would be nice to see a Win32 documentation page
that explicitly said that.
+ /* For reparse points dwReserved0 field will contain the ReparseTag */
+ else if ((fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0
+ && (fd.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT))
+ d->ret.d_type = DT_LNK;
Hmm, it's interesting that our existing test for a junction in
pgwin32_is_junction() only looks for FILE_ATTRIBUTE_REPARSE_POINT and
doesn't care what kind of reparse point it is.
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-06 22:36 Tom Lane <tgl@sss.pgh.pa.us>
parent: Thomas Munro <thomas.munro@gmail.com>
1 sibling, 1 reply; 131+ messages in thread
From: Tom Lane @ 2020-09-06 22:36 UTC (permalink / raw)
To: Thomas Munro <thomas.munro@gmail.com>; +Cc: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>; Andres Freund <andres@anarazel.de>; pgsql-hackers
Thomas Munro <thomas.munro@gmail.com> writes:
> Excellent. I'd like to commit these soon, unless someone has a better
> idea for how to name file_utils_febe.c.
As long as it's in src/port or src/common, isn't it implicit that
it's probably FE/BE common code?
I think it'd make more sense to insert all this stuff into file_utils.c,
and then just "#ifdef FRONTEND" the existing code there that doesn't work
in backend. For one thing, that gives us a saner upgrade path whenever
somebody gets ambitious enough to make that code work for the backend.
regards, tom lane
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-07 06:29 Thomas Munro <thomas.munro@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 131+ messages in thread
From: Thomas Munro @ 2020-09-07 06:29 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>; Andres Freund <andres@anarazel.de>; pgsql-hackers
On Mon, Sep 7, 2020 at 10:36 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Thomas Munro <thomas.munro@gmail.com> writes:
> > Excellent. I'd like to commit these soon, unless someone has a better
> > idea for how to name file_utils_febe.c.
>
> As long as it's in src/port or src/common, isn't it implicit that
> it's probably FE/BE common code?
>
> I think it'd make more sense to insert all this stuff into file_utils.c,
> and then just "#ifdef FRONTEND" the existing code there that doesn't work
> in backend. For one thing, that gives us a saner upgrade path whenever
> somebody gets ambitious enough to make that code work for the backend.
True. Ok, I committed the Unix patch that way. I know it works on
Linux, FreeBSD, macOS and Windows (without Juan José's patch), but
I'll have to check the build farm later to make sure HPUX, AIX and
Solaris are OK with this. It's remotely possible that one of them
defines DT_REG etc but doesn't have d_type; I'm hoping to get away
with not adding a configure check.
I'll wait a bit longer for comments on the Windows patch.
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-07 09:42 Magnus Hagander <magnus@hagander.net>
parent: Thomas Munro <thomas.munro@gmail.com>
1 sibling, 1 reply; 131+ messages in thread
From: Magnus Hagander @ 2020-09-07 09:42 UTC (permalink / raw)
To: Thomas Munro <thomas.munro@gmail.com>; +Cc: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>; Andres Freund <andres@anarazel.de>; Tom Lane <tgl@sss.pgh.pa.us>; pgsql-hackers
On Mon, Sep 7, 2020 at 12:27 AM Thomas Munro <thomas.munro@gmail.com> wrote:
> On Sun, Sep 6, 2020 at 5:23 AM Juan José Santamaría Flecha
> <juanjo.santamaria@gmail.com> wrote:
> > On Sat, Sep 5, 2020 at 2:13 AM Andres Freund <andres@anarazel.de> wrote:
> >> > However, it looks like we might be missing a further opportunity
> >> > here... Doesn't Windows already give us the flags we need in the
> >> > dwFileAttributes member of the WIN32_FIND_DATA object that the
> >> > Find{First,Next}File() functions populate?
> >>
> >> That'd be better...
> >
> >
> > At first I did not see how to get DT_LNK directly, but it is possible
> without additional calls, so please find attached a version with that logic.
> >
> > This version also drops the enum, defining just the macros.
>
> Excellent. I'd like to commit these soon, unless someone has a better
> idea for how to name file_utils_febe.c.
>
> I think the following is a little mysterious, but it does seem to be
> what people do for this in other projects. It is the documented way
> to detect mount points, and I guess IO_REPARSE_TAG_MOUNT_POINT is
> either overloaded also for junctions, or junctions are the same thing
> as mount points. It would be nice to see a Win32 documentation page
> that explicitly said that.
>
The wikipedia page on it is actually fairly decent:
https://en.wikipedia.org/wiki/NTFS_reparse_point. It's not the
documentation of course, but it's easier to read :) The core difference is
whether you mount a whole filesystem (mount point) or just a directory off
something mounted elsehwere (junction).
And yes, the wikipedia page confirms that junctions also use
IO_REPARSE_TAG_MOUNT_POINT.
+ /* For reparse points dwReserved0 field will contain the ReparseTag */
> + else if ((fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0
> + && (fd.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT))
> + d->ret.d_type = DT_LNK;
>
> Hmm, it's interesting that our existing test for a junction in
> pgwin32_is_junction() only looks for FILE_ATTRIBUTE_REPARSE_POINT and
> doesn't care what kind of reparse point it is.
>
I think that's mostly historical. When that code was written, the only two
types of reparse points that existed were junctions and mount points --
which are as already noted, the same. Symbolic links, unix sockets and such
things came later.
--
Magnus Hagander
Me: https://www.hagander.net/ <http://www.hagander.net/;
Work: https://www.redpill-linpro.com/ <http://www.redpill-linpro.com/;
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-07 11:40 Thomas Munro <thomas.munro@gmail.com>
parent: Magnus Hagander <magnus@hagander.net>
0 siblings, 1 reply; 131+ messages in thread
From: Thomas Munro @ 2020-09-07 11:40 UTC (permalink / raw)
To: Magnus Hagander <magnus@hagander.net>; +Cc: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>; Andres Freund <andres@anarazel.de>; Tom Lane <tgl@sss.pgh.pa.us>; pgsql-hackers
On Mon, Sep 7, 2020 at 9:42 PM Magnus Hagander <magnus@hagander.net> wrote:
> On Mon, Sep 7, 2020 at 12:27 AM Thomas Munro <thomas.munro@gmail.com> wrote:
>> I think the following is a little mysterious, but it does seem to be
>> what people do for this in other projects. It is the documented way
>> to detect mount points, and I guess IO_REPARSE_TAG_MOUNT_POINT is
>> either overloaded also for junctions, or junctions are the same thing
>> as mount points. It would be nice to see a Win32 documentation page
>> that explicitly said that.
>
> The wikipedia page on it is actually fairly decent: https://en.wikipedia.org/wiki/NTFS_reparse_point. It's not the documentation of course, but it's easier to read :) The core difference is whether you mount a whole filesystem (mount point) or just a directory off something mounted elsehwere (junction).
>
> And yes, the wikipedia page confirms that junctions also use IO_REPARSE_TAG_MOUNT_POINT.
Thanks for confirming. I ran the Windows patch through pgindent,
fixed a small typo, and pushed.
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-07 14:03 Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>
parent: Thomas Munro <thomas.munro@gmail.com>
0 siblings, 1 reply; 131+ messages in thread
From: Juan José Santamaría Flecha @ 2020-09-07 14:03 UTC (permalink / raw)
To: Thomas Munro <thomas.munro@gmail.com>; +Cc: Magnus Hagander <magnus@hagander.net>; Andres Freund <andres@anarazel.de>; Tom Lane <tgl@sss.pgh.pa.us>; pgsql-hackers
On Mon, Sep 7, 2020 at 1:41 PM Thomas Munro <thomas.munro@gmail.com> wrote:
> On Mon, Sep 7, 2020 at 9:42 PM Magnus Hagander <magnus@hagander.net>
> wrote:
> > On Mon, Sep 7, 2020 at 12:27 AM Thomas Munro <thomas.munro@gmail.com>
> wrote:
> >> I think the following is a little mysterious, but it does seem to be
> >> what people do for this in other projects. It is the documented way
> >> to detect mount points, and I guess IO_REPARSE_TAG_MOUNT_POINT is
> >> either overloaded also for junctions, or junctions are the same thing
> >> as mount points. It would be nice to see a Win32 documentation page
> >> that explicitly said that.
> >
> > The wikipedia page on it is actually fairly decent:
> https://en.wikipedia.org/wiki/NTFS_reparse_point. It's not the
> documentation of course, but it's easier to read :) The core difference is
> whether you mount a whole filesystem (mount point) or just a directory off
> something mounted elsehwere (junction).
> >
> > And yes, the wikipedia page confirms that junctions also use
> IO_REPARSE_TAG_MOUNT_POINT.
>
> Thanks for confirming. I ran the Windows patch through pgindent,
> fixed a small typo, and pushed.
>
Great, thanks. Should we include something from this discussion as comments?
Regards,
Juan José Santamaría Flecha
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-07 22:03 Thomas Munro <thomas.munro@gmail.com>
parent: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>
0 siblings, 1 reply; 131+ messages in thread
From: Thomas Munro @ 2020-09-07 22:03 UTC (permalink / raw)
To: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>; +Cc: Magnus Hagander <magnus@hagander.net>; Andres Freund <andres@anarazel.de>; Tom Lane <tgl@sss.pgh.pa.us>; pgsql-hackers
On Tue, Sep 8, 2020 at 2:05 AM Juan José Santamaría Flecha
<juanjo.santamaria@gmail.com> wrote:
> On Mon, Sep 7, 2020 at 1:41 PM Thomas Munro <thomas.munro@gmail.com> wrote:
>> Thanks for confirming. I ran the Windows patch through pgindent,
>> fixed a small typo, and pushed.
>
> Great, thanks. Should we include something from this discussion as comments?
I dunno, it seems like this may be common knowledge for Windows people
and I was just being paranoid by asking for more info as a
non-Windowsian, but if you want to propose new comments or perhaps
just a pointer to one central place where we explain how that works, I
wouldn't be against that.
FWIW I just spotted a couple of very suspicious looking failures for
build farm animal "walleye", a "MinGW64 8.1.0" system, that say:
c:\\pgbuildfarm\\pgbuildroot\\HEAD\\pgsql.build\\src\\bin\\pg_upgrade>RMDIR
/s/q "c:\\pgbuildfarm\\pgbuildroot\\HEAD\\pgsql.build\\src\\bin\\pg_upgrade\\tmp_check\\data.old"
The directory is not empty.
Then I noticed it failed the same way 8 days ago, so I don't know
what's up with that but it looks like we didn't break it...
^ permalink raw reply [nested|flat] 131+ messages in thread
* Re: A micro-optimisation for walkdir()
@ 2020-09-07 22:53 Tom Lane <tgl@sss.pgh.pa.us>
parent: Thomas Munro <thomas.munro@gmail.com>
0 siblings, 0 replies; 131+ messages in thread
From: Tom Lane @ 2020-09-07 22:53 UTC (permalink / raw)
To: Thomas Munro <thomas.munro@gmail.com>; +Cc: Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>; Magnus Hagander <magnus@hagander.net>; Andres Freund <andres@anarazel.de>; pgsql-hackers
Thomas Munro <thomas.munro@gmail.com> writes:
> FWIW I just spotted a couple of very suspicious looking failures for
> build farm animal "walleye", a "MinGW64 8.1.0" system, that say:
walleye's been kind of unstable since the get-go, so I wouldn't put
too much faith in reports just from it.
regards, tom lane
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
* [PATCH v8 3/3] Use new overflow-aware integer comparison functions.
@ 2024-02-15 23:28 Nathan Bossart <nathan@postgresql.org>
0 siblings, 0 replies; 131+ messages in thread
From: Nathan Bossart @ 2024-02-15 23:28 UTC (permalink / raw)
Commit XXXXXXXXXX introduced integer comparison functions designed
to be as efficient as possible while avoiding overflow. This
commit makes use of these functions in many of the in-tree qsort()
comparators, which helps ensure transitivity. The comparator
functions that previously had trivial implementations (i.e., "if"
statements) should also see a small performance boost.
Author: Mats Kindahl
Reviewed-by: Tom Lane, Heikki Linnakangas, Andres Freund, Fabrzio de Royes Mello
Discussion: https://postgr.es/m/CA%2B14426g2Wa9QuUpmakwPxXFWG_1FaY0AsApkvcTBy-YfS6uaw%40mail.gmail.com
---
contrib/hstore/hstore_gist.c | 4 +++-
contrib/intarray/_int_tool.c | 9 +++------
contrib/intarray/_intbig_gist.c | 4 +++-
contrib/pg_stat_statements/pg_stat_statements.c | 8 ++------
contrib/pg_trgm/trgm_op.c | 8 ++------
src/backend/access/nbtree/nbtinsert.c | 8 ++------
src/backend/access/nbtree/nbtpage.c | 10 +++-------
src/backend/access/nbtree/nbtsplitloc.c | 8 ++------
src/backend/access/spgist/spgdoinsert.c | 5 ++---
src/backend/access/spgist/spgtextproc.c | 3 ++-
src/backend/backup/basebackup_incremental.c | 8 ++------
src/backend/backup/walsummary.c | 7 ++-----
src/backend/catalog/heap.c | 7 ++-----
src/backend/nodes/list.c | 13 +++----------
src/backend/nodes/tidbitmap.c | 7 ++-----
src/backend/parser/parse_agg.c | 3 ++-
src/backend/postmaster/autovacuum.c | 7 +++----
src/backend/replication/logical/reorderbuffer.c | 7 ++-----
src/backend/replication/syncrep.c | 8 ++------
src/backend/utils/adt/oid.c | 7 ++-----
src/backend/utils/adt/tsgistidx.c | 10 +++-------
src/backend/utils/adt/tsquery_gist.c | 7 +++----
src/backend/utils/adt/tsvector.c | 5 ++---
src/backend/utils/adt/tsvector_op.c | 5 ++---
src/backend/utils/adt/xid.c | 7 ++-----
src/backend/utils/cache/relcache.c | 3 ++-
src/backend/utils/cache/syscache.c | 5 ++---
src/backend/utils/cache/typcache.c | 8 ++------
src/backend/utils/resowner/resowner.c | 10 ++--------
src/bin/pg_dump/pg_dump_sort.c | 7 ++-----
src/bin/pg_upgrade/function.c | 12 ++++++------
src/bin/pg_walsummary/pg_walsummary.c | 8 ++------
src/bin/psql/crosstabview.c | 3 ++-
src/include/access/gin_private.h | 8 ++------
34 files changed, 80 insertions(+), 159 deletions(-)
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index fe343739eb..a3b08af385 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -7,6 +7,7 @@
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "hstore.h"
#include "utils/pg_crc.h"
@@ -356,7 +357,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c
index 68f624e085..c85280c842 100644
--- a/contrib/intarray/_int_tool.c
+++ b/contrib/intarray/_int_tool.c
@@ -7,6 +7,7 @@
#include "_int.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
/* arguments are assumed sorted & unique-ified */
@@ -396,15 +397,11 @@ int_to_intset(int32 elem)
int
compASC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a > *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) a, *(const int32 *) b);
}
int
compDESC(const void *a, const void *b)
{
- if (*(const int32 *) a == *(const int32 *) b)
- return 0;
- return (*(const int32 *) a < *(const int32 *) b) ? 1 : -1;
+ return pg_cmp_s32(*(const int32 *) b, *(const int32 *) a);
}
diff --git a/contrib/intarray/_intbig_gist.c b/contrib/intarray/_intbig_gist.c
index 8c6c4b5d05..9699fbf3b4 100644
--- a/contrib/intarray/_intbig_gist.c
+++ b/contrib/intarray/_intbig_gist.c
@@ -9,6 +9,7 @@
#include "access/gist.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "port/pg_bitutils.h"
#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
@@ -312,7 +313,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index 8c6a3a2d08..67cec865ba 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -50,6 +50,7 @@
#include "access/parallel.h"
#include "catalog/pg_authid.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "executor/instrument.h"
#include "funcapi.h"
#include "jit/jit.h"
@@ -3007,10 +3008,5 @@ comp_location(const void *a, const void *b)
int l = ((const LocationLen *) a)->location;
int r = ((const LocationLen *) b)->location;
- if (l < r)
- return -1;
- else if (l > r)
- return +1;
- else
- return 0;
+ return pg_cmp_s32(l, r);
}
diff --git a/contrib/pg_trgm/trgm_op.c b/contrib/pg_trgm/trgm_op.c
index 49d4497b4f..c509d15ee4 100644
--- a/contrib/pg_trgm/trgm_op.c
+++ b/contrib/pg_trgm/trgm_op.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
@@ -433,12 +434,7 @@ comp_ptrgm(const void *v1, const void *v2)
if (cmp != 0)
return cmp;
- if (p1->index < p2->index)
- return -1;
- else if (p1->index == p2->index)
- return 0;
- else
- return 1;
+ return pg_cmp_s32(p1->index, p2->index);
}
/*
diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 709edd1c17..e9cfc13604 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -19,6 +19,7 @@
#include "access/nbtxlog.h"
#include "access/transam.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "lib/qunique.h"
#include "miscadmin.h"
@@ -3013,10 +3014,5 @@ _bt_blk_cmp(const void *arg1, const void *arg2)
BlockNumber b1 = *((BlockNumber *) arg1);
BlockNumber b2 = *((BlockNumber *) arg2);
- if (b1 < b2)
- return -1;
- else if (b1 > b2)
- return 1;
-
- return 0;
+ return pg_cmp_u32(b1, b2);
}
diff --git a/src/backend/access/nbtree/nbtpage.c b/src/backend/access/nbtree/nbtpage.c
index 567bade9f4..90990ea77a 100644
--- a/src/backend/access/nbtree/nbtpage.c
+++ b/src/backend/access/nbtree/nbtpage.c
@@ -28,6 +28,7 @@
#include "access/transam.h"
#include "access/xlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "storage/indexfsm.h"
#include "storage/lmgr.h"
@@ -1466,14 +1467,9 @@ _bt_delitems_cmp(const void *a, const void *b)
TM_IndexDelete *indexdelete1 = (TM_IndexDelete *) a;
TM_IndexDelete *indexdelete2 = (TM_IndexDelete *) b;
- if (indexdelete1->id > indexdelete2->id)
- return 1;
- if (indexdelete1->id < indexdelete2->id)
- return -1;
+ Assert(indexdelete1->id != indexdelete2->id);
- Assert(false);
-
- return 0;
+ return pg_cmp_s16(indexdelete1->id, indexdelete2->id);
}
/*
diff --git a/src/backend/access/nbtree/nbtsplitloc.c b/src/backend/access/nbtree/nbtsplitloc.c
index d0b1d82578..490e7bfd4d 100644
--- a/src/backend/access/nbtree/nbtsplitloc.c
+++ b/src/backend/access/nbtree/nbtsplitloc.c
@@ -15,6 +15,7 @@
#include "postgres.h"
#include "access/nbtree.h"
+#include "common/int.h"
#include "storage/lmgr.h"
typedef enum
@@ -596,12 +597,7 @@ _bt_splitcmp(const void *arg1, const void *arg2)
SplitPoint *split1 = (SplitPoint *) arg1;
SplitPoint *split2 = (SplitPoint *) arg2;
- if (split1->curdelta > split2->curdelta)
- return 1;
- if (split1->curdelta < split2->curdelta)
- return -1;
-
- return 0;
+ return pg_cmp_s16(split1->curdelta, split2->curdelta);
}
/*
diff --git a/src/backend/access/spgist/spgdoinsert.c b/src/backend/access/spgist/spgdoinsert.c
index bb063c858d..a4995c168b 100644
--- a/src/backend/access/spgist/spgdoinsert.c
+++ b/src/backend/access/spgist/spgdoinsert.c
@@ -19,6 +19,7 @@
#include "access/spgist_private.h"
#include "access/spgxlog.h"
#include "access/xloginsert.h"
+#include "common/int.h"
#include "common/pg_prng.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
@@ -110,9 +111,7 @@ addNode(SpGistState *state, SpGistInnerTuple tuple, Datum label, int offset)
static int
cmpOffsetNumbers(const void *a, const void *b)
{
- if (*(const OffsetNumber *) a == *(const OffsetNumber *) b)
- return 0;
- return (*(const OffsetNumber *) a > *(const OffsetNumber *) b) ? 1 : -1;
+ return pg_cmp_u16(*(const OffsetNumber *) a, *(const OffsetNumber *) b);
}
/*
diff --git a/src/backend/access/spgist/spgtextproc.c b/src/backend/access/spgist/spgtextproc.c
index b8fd0c2ad8..d5db5225a9 100644
--- a/src/backend/access/spgist/spgtextproc.c
+++ b/src/backend/access/spgist/spgtextproc.c
@@ -40,6 +40,7 @@
#include "postgres.h"
#include "access/spgist.h"
+#include "common/int.h"
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "utils/builtins.h"
@@ -325,7 +326,7 @@ cmpNodePtr(const void *a, const void *b)
const spgNodePtr *aa = (const spgNodePtr *) a;
const spgNodePtr *bb = (const spgNodePtr *) b;
- return aa->c - bb->c;
+ return pg_cmp_s16(aa->c, bb->c);
}
Datum
diff --git a/src/backend/backup/basebackup_incremental.c b/src/backend/backup/basebackup_incremental.c
index 0504c465db..e994ee66bb 100644
--- a/src/backend/backup/basebackup_incremental.c
+++ b/src/backend/backup/basebackup_incremental.c
@@ -27,6 +27,7 @@
#include "common/blkreftable.h"
#include "common/parse_manifest.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "postmaster/walsummarizer.h"
#define BLOCKS_PER_READ 512
@@ -994,10 +995,5 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
diff --git a/src/backend/backup/walsummary.c b/src/backend/backup/walsummary.c
index 867870aaad..4d047e1c02 100644
--- a/src/backend/backup/walsummary.c
+++ b/src/backend/backup/walsummary.c
@@ -17,6 +17,7 @@
#include "access/xlog_internal.h"
#include "backup/walsummary.h"
+#include "common/int.h"
#include "utils/wait_event.h"
static bool IsWalSummaryFilename(char *filename);
@@ -355,9 +356,5 @@ ListComparatorForWalSummaryFiles(const ListCell *a, const ListCell *b)
WalSummaryFile *ws1 = lfirst(a);
WalSummaryFile *ws2 = lfirst(b);
- if (ws1->start_lsn < ws2->start_lsn)
- return -1;
- if (ws1->start_lsn > ws2->start_lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(ws1->start_lsn, ws2->start_lsn);
}
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index c73f7bcd01..03368b93d7 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -56,6 +56,7 @@
#include "catalog/storage.h"
#include "commands/tablecmds.h"
#include "commands/typecmds.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -2759,11 +2760,7 @@ list_cookedconstr_attnum_cmp(const ListCell *p1, const ListCell *p2)
AttrNumber v1 = ((CookedConstraint *) lfirst(p1))->attnum;
AttrNumber v2 = ((CookedConstraint *) lfirst(p2))->attnum;
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s16(v1, v2);
}
/*
diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c
index 957186bef5..e2615ab105 100644
--- a/src/backend/nodes/list.c
+++ b/src/backend/nodes/list.c
@@ -17,6 +17,7 @@
*/
#include "postgres.h"
+#include "common/int.h"
#include "nodes/pg_list.h"
#include "port/pg_bitutils.h"
#include "utils/memdebug.h"
@@ -1692,11 +1693,7 @@ list_int_cmp(const ListCell *p1, const ListCell *p2)
int v1 = lfirst_int(p1);
int v2 = lfirst_int(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_s32(v1, v2);
}
/*
@@ -1708,9 +1705,5 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2)
Oid v1 = lfirst_oid(p1);
Oid v2 = lfirst_oid(p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/nodes/tidbitmap.c b/src/backend/nodes/tidbitmap.c
index 0f4850065f..e8ab5d78fc 100644
--- a/src/backend/nodes/tidbitmap.c
+++ b/src/backend/nodes/tidbitmap.c
@@ -42,6 +42,7 @@
#include "access/htup_details.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "nodes/bitmapset.h"
#include "nodes/tidbitmap.h"
#include "storage/lwlock.h"
@@ -1425,11 +1426,7 @@ tbm_comparator(const void *left, const void *right)
BlockNumber l = (*((PagetableEntry *const *) left))->blockno;
BlockNumber r = (*((PagetableEntry *const *) right))->blockno;
- if (l < r)
- return -1;
- else if (l > r)
- return 1;
- return 0;
+ return pg_cmp_u32(l, r);
}
/*
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 7b211a7743..9d151a880b 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -18,6 +18,7 @@
#include "catalog/pg_aggregate.h"
#include "catalog/pg_constraint.h"
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
@@ -1760,7 +1761,7 @@ cmp_list_len_asc(const ListCell *a, const ListCell *b)
int la = list_length((const List *) lfirst(a));
int lb = list_length((const List *) lfirst(b));
- return (la > lb) ? 1 : (la == lb) ? 0 : -1;
+ return pg_cmp_s32(la, lb);
}
/* list_sort comparator to sort sub-lists by length and contents */
diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 37998f7387..2ab344c1f8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -78,6 +78,7 @@
#include "catalog/pg_database.h"
#include "commands/dbcommands.h"
#include "commands/vacuum.h"
+#include "common/int.h"
#include "lib/ilist.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
@@ -1120,10 +1121,8 @@ rebuild_database_list(Oid newdb)
static int
db_comparator(const void *a, const void *b)
{
- if (((const avl_dbase *) a)->adl_score == ((const avl_dbase *) b)->adl_score)
- return 0;
- else
- return (((const avl_dbase *) a)->adl_score < ((const avl_dbase *) b)->adl_score) ? 1 : -1;
+ return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
+ ((const avl_dbase *) b)->adl_score);
}
/*
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index bbf0966182..5446df3c64 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -91,6 +91,7 @@
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/catalog.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "miscadmin.h"
#include "pgstat.h"
@@ -5119,11 +5120,7 @@ file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
- if (a->lsn < b->lsn)
- return -1;
- else if (a->lsn > b->lsn)
- return 1;
- return 0;
+ return pg_cmp_u64(a->lsn, b->lsn);
}
/*
diff --git a/src/backend/replication/syncrep.c b/src/backend/replication/syncrep.c
index 2e6493aaaa..bfcd8fa13e 100644
--- a/src/backend/replication/syncrep.c
+++ b/src/backend/replication/syncrep.c
@@ -75,6 +75,7 @@
#include <unistd.h>
#include "access/xact.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/syncrep.h"
@@ -698,12 +699,7 @@ cmp_lsn(const void *a, const void *b)
XLogRecPtr lsn1 = *((const XLogRecPtr *) a);
XLogRecPtr lsn2 = *((const XLogRecPtr *) b);
- if (lsn1 > lsn2)
- return -1;
- else if (lsn1 == lsn2)
- return 0;
- else
- return 1;
+ return pg_cmp_u64(lsn2, lsn1);
}
/*
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index 62bcfc5b56..56fb1fd77c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -18,6 +18,7 @@
#include <limits.h>
#include "catalog/pg_type.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "nodes/value.h"
@@ -259,11 +260,7 @@ oid_cmp(const void *p1, const void *p2)
Oid v1 = *((const Oid *) p1);
Oid v2 = *((const Oid *) p2);
- if (v1 < v2)
- return -1;
- if (v1 > v2)
- return 1;
- return 0;
+ return pg_cmp_u32(v1, v2);
}
diff --git a/src/backend/utils/adt/tsgistidx.c b/src/backend/utils/adt/tsgistidx.c
index a62b285365..3fb7696434 100644
--- a/src/backend/utils/adt/tsgistidx.c
+++ b/src/backend/utils/adt/tsgistidx.c
@@ -17,6 +17,7 @@
#include "access/gist.h"
#include "access/heaptoast.h"
#include "access/reloptions.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "port/pg_bitutils.h"
#include "tsearch/ts_utils.h"
@@ -136,9 +137,7 @@ compareint(const void *va, const void *vb)
int32 a = *((const int32 *) va);
int32 b = *((const int32 *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static void
@@ -598,10 +597,7 @@ comparecost(const void *va, const void *vb)
const SPLITCOST *a = (const SPLITCOST *) va;
const SPLITCOST *b = (const SPLITCOST *) vb;
- if (a->cost == b->cost)
- return 0;
- else
- return (a->cost > b->cost) ? 1 : -1;
+ return pg_cmp_s32(a->cost, b->cost);
}
diff --git a/src/backend/utils/adt/tsquery_gist.c b/src/backend/utils/adt/tsquery_gist.c
index f0b1c81c81..2db304b10b 100644
--- a/src/backend/utils/adt/tsquery_gist.c
+++ b/src/backend/utils/adt/tsquery_gist.c
@@ -16,6 +16,7 @@
#include "access/gist.h"
#include "access/stratnum.h"
+#include "common/int.h"
#include "tsearch/ts_utils.h"
#include "utils/builtins.h"
@@ -156,10 +157,8 @@ typedef struct
static int
comparecost(const void *a, const void *b)
{
- if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
- return 0;
- else
- return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
+ return pg_cmp_s32(((const SPLITCOST *) a)->cost,
+ ((const SPLITCOST *) b)->cost);
}
#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb7b7c712a..10bc4f2234 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -14,6 +14,7 @@
#include "postgres.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "nodes/miscnodes.h"
#include "tsearch/ts_locale.h"
@@ -37,9 +38,7 @@ compareWordEntryPos(const void *a, const void *b)
int apos = WEP_GETPOS(*(const WordEntryPos *) a);
int bpos = WEP_GETPOS(*(const WordEntryPos *) b);
- if (apos == bpos)
- return 0;
- return (apos > bpos) ? 1 : -1;
+ return pg_cmp_s32(apos, bpos);
}
/*
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 71386d0a1f..947a592ed2 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -19,6 +19,7 @@
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/qunique.h"
@@ -435,9 +436,7 @@ compare_int(const void *va, const void *vb)
int a = *((const int *) va);
int b = *((const int *) vb);
- if (a == b)
- return 0;
- return (a > b) ? 1 : -1;
+ return pg_cmp_s32(a, b);
}
static int
diff --git a/src/backend/utils/adt/xid.c b/src/backend/utils/adt/xid.c
index 63adf5668b..ae273b1961 100644
--- a/src/backend/utils/adt/xid.c
+++ b/src/backend/utils/adt/xid.c
@@ -19,6 +19,7 @@
#include "access/multixact.h"
#include "access/transam.h"
#include "access/xact.h"
+#include "common/int.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
#include "utils/xid8.h"
@@ -140,11 +141,7 @@ xidComparator(const void *arg1, const void *arg2)
TransactionId xid1 = *(const TransactionId *) arg1;
TransactionId xid2 = *(const TransactionId *) arg2;
- if (xid1 > xid2)
- return 1;
- if (xid1 < xid2)
- return -1;
- return 0;
+ return pg_cmp_u32(xid1, xid2);
}
/*
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index ac106b40e3..50acae4529 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -69,6 +69,7 @@
#include "commands/policy.h"
#include "commands/publicationcmds.h"
#include "commands/trigger.h"
+#include "common/int.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
@@ -4520,7 +4521,7 @@ AttrDefaultCmp(const void *a, const void *b)
const AttrDefault *ada = (const AttrDefault *) a;
const AttrDefault *adb = (const AttrDefault *) b;
- return ada->adnum - adb->adnum;
+ return pg_cmp_s16(ada->adnum, adb->adnum);
}
/*
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index 662f930864..2292237f85 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -29,6 +29,7 @@
#include "catalog/pg_shdepend_d.h"
#include "catalog/pg_shdescription_d.h"
#include "catalog/pg_shseclabel_d.h"
+#include "common/int.h"
#include "lib/qunique.h"
#include "utils/catcache.h"
#include "utils/lsyscache.h"
@@ -676,7 +677,5 @@ oid_compare(const void *a, const void *b)
Oid oa = *((const Oid *) a);
Oid ob = *((const Oid *) b);
- if (oa == ob)
- return 0;
- return (oa > ob) ? 1 : -1;
+ return pg_cmp_u32(oa, ob);
}
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index 84fc83cb11..2842bde907 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -57,6 +57,7 @@
#include "catalog/pg_range.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
+#include "common/int.h"
#include "executor/executor.h"
#include "lib/dshash.h"
#include "optimizer/optimizer.h"
@@ -2722,12 +2723,7 @@ enum_oid_cmp(const void *left, const void *right)
const EnumItem *l = (const EnumItem *) left;
const EnumItem *r = (const EnumItem *) right;
- if (l->enum_oid < r->enum_oid)
- return -1;
- else if (l->enum_oid > r->enum_oid)
- return 1;
- else
- return 0;
+ return pg_cmp_u32(l->enum_oid, r->enum_oid);
}
/*
diff --git a/src/backend/utils/resowner/resowner.c b/src/backend/utils/resowner/resowner.c
index aa199b23ff..ab9343bc5c 100644
--- a/src/backend/utils/resowner/resowner.c
+++ b/src/backend/utils/resowner/resowner.c
@@ -46,6 +46,7 @@
#include "postgres.h"
#include "common/hashfn.h"
+#include "common/int.h"
#include "storage/ipc.h"
#include "storage/predicate.h"
#include "storage/proc.h"
@@ -264,14 +265,7 @@ resource_priority_cmp(const void *a, const void *b)
/* Note: reverse order */
if (ra->kind->release_phase == rb->kind->release_phase)
- {
- if (ra->kind->release_priority == rb->kind->release_priority)
- return 0;
- else if (ra->kind->release_priority > rb->kind->release_priority)
- return -1;
- else
- return 1;
- }
+ return pg_cmp_u32(rb->kind->release_priority, ra->kind->release_priority);
else if (ra->kind->release_phase > rb->kind->release_phase)
return -1;
else
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index f358dd22b9..8ee8a42781 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -16,6 +16,7 @@
#include "postgres_fe.h"
#include "catalog/pg_class_d.h"
+#include "common/int.h"
#include "lib/binaryheap.h"
#include "pg_backup_archiver.h"
#include "pg_backup_utils.h"
@@ -1504,9 +1505,5 @@ int_cmp(void *a, void *b, void *arg)
int ai = (int) (intptr_t) a;
int bi = (int) (intptr_t) b;
- if (ai < bi)
- return -1;
- if (ai > bi)
- return 1;
- return 0;
+ return pg_cmp_s32(ai, bi);
}
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index af998f74d3..d65153de81 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -11,6 +11,7 @@
#include "access/transam.h"
#include "catalog/pg_language_d.h"
+#include "common/int.h"
#include "pg_upgrade.h"
/*
@@ -29,17 +30,16 @@ library_name_compare(const void *p1, const void *p2)
{
const char *str1 = ((const LibraryInfo *) p1)->name;
const char *str2 = ((const LibraryInfo *) p2)->name;
- int slen1 = strlen(str1);
- int slen2 = strlen(str2);
+ size_t slen1 = strlen(str1);
+ size_t slen2 = strlen(str2);
int cmp = strcmp(str1, str2);
if (slen1 != slen2)
- return slen1 - slen2;
+ return pg_cmp_size(slen1, slen2);
if (cmp != 0)
return cmp;
- else
- return ((const LibraryInfo *) p1)->dbnum -
- ((const LibraryInfo *) p2)->dbnum;
+ return pg_cmp_s32(((const LibraryInfo *) p1)->dbnum,
+ ((const LibraryInfo *) p2)->dbnum);
}
diff --git a/src/bin/pg_walsummary/pg_walsummary.c b/src/bin/pg_walsummary/pg_walsummary.c
index 1341c83c69..485c72d939 100644
--- a/src/bin/pg_walsummary/pg_walsummary.c
+++ b/src/bin/pg_walsummary/pg_walsummary.c
@@ -16,6 +16,7 @@
#include <limits.h>
#include "common/blkreftable.h"
+#include "common/int.h"
#include "common/logging.h"
#include "fe_utils/option_utils.h"
#include "lib/stringinfo.h"
@@ -219,12 +220,7 @@ compare_block_numbers(const void *a, const void *b)
BlockNumber aa = *(BlockNumber *) a;
BlockNumber bb = *(BlockNumber *) b;
- if (aa > bb)
- return 1;
- else if (aa == bb)
- return 0;
- else
- return -1;
+ return pg_cmp_u32(aa, bb);
}
/*
diff --git a/src/bin/psql/crosstabview.c b/src/bin/psql/crosstabview.c
index c6116b7238..305ed4ab0a 100644
--- a/src/bin/psql/crosstabview.c
+++ b/src/bin/psql/crosstabview.c
@@ -8,6 +8,7 @@
#include "postgres_fe.h"
#include "common.h"
+#include "common/int.h"
#include "common/logging.h"
#include "crosstabview.h"
#include "pqexpbuffer.h"
@@ -709,5 +710,5 @@ pivotFieldCompare(const void *a, const void *b)
static int
rankCompare(const void *a, const void *b)
{
- return *((const int *) a) - *((const int *) b);
+ return pg_cmp_s32(*(const int *) a, *(const int *) b);
}
diff --git a/src/include/access/gin_private.h b/src/include/access/gin_private.h
index 51d0c74a6b..3013a44bae 100644
--- a/src/include/access/gin_private.h
+++ b/src/include/access/gin_private.h
@@ -14,6 +14,7 @@
#include "access/gin.h"
#include "access/ginblock.h"
#include "access/itup.h"
+#include "common/int.h"
#include "catalog/pg_am_d.h"
#include "fmgr.h"
#include "lib/rbtree.h"
@@ -489,12 +490,7 @@ ginCompareItemPointers(ItemPointer a, ItemPointer b)
uint64 ia = (uint64) GinItemPointerGetBlockNumber(a) << 32 | GinItemPointerGetOffsetNumber(a);
uint64 ib = (uint64) GinItemPointerGetBlockNumber(b) << 32 | GinItemPointerGetOffsetNumber(b);
- if (ia == ib)
- return 0;
- else if (ia > ib)
- return 1;
- else
- return -1;
+ return pg_cmp_u64(ia, ib);
}
extern int ginTraverseLock(Buffer buffer, bool searchMode);
--
2.25.1
--AhhlLboLdkugWU4S--
^ permalink raw reply [nested|flat] 131+ messages in thread
end of thread, other threads:[~2024-02-15 23:28 UTC | newest]
Thread overview: 131+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-09-02 05:18 A micro-optimisation for walkdir() Thomas Munro <thomas.munro@gmail.com>
2020-09-02 14:34 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-09-02 15:51 ` Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>
2020-09-03 05:08 ` Thomas Munro <thomas.munro@gmail.com>
2020-09-03 05:36 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-09-03 06:38 ` Thomas Munro <thomas.munro@gmail.com>
2020-09-03 15:31 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-09-03 23:58 ` Thomas Munro <thomas.munro@gmail.com>
2020-09-04 19:37 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2020-09-04 20:03 ` Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>
2020-09-04 20:28 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2020-09-04 20:34 ` Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>
2020-09-04 20:43 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2020-09-04 21:39 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-09-04 21:52 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2020-09-04 21:45 ` Andres Freund <andres@anarazel.de>
2020-09-04 23:15 ` Thomas Munro <thomas.munro@gmail.com>
2020-09-05 00:13 ` Andres Freund <andres@anarazel.de>
2020-09-05 17:22 ` Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>
2020-09-06 22:26 ` Thomas Munro <thomas.munro@gmail.com>
2020-09-06 22:36 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-09-07 06:29 ` Thomas Munro <thomas.munro@gmail.com>
2020-09-07 09:42 ` Magnus Hagander <magnus@hagander.net>
2020-09-07 11:40 ` Thomas Munro <thomas.munro@gmail.com>
2020-09-07 14:03 ` Juan José Santamaría Flecha <juanjo.santamaria@gmail.com>
2020-09-07 22:03 ` Thomas Munro <thomas.munro@gmail.com>
2020-09-07 22:53 ` Tom Lane <tgl@sss.pgh.pa.us>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
2024-02-15 23:28 [PATCH v8 3/3] Use new overflow-aware integer comparison functions. Nathan Bossart <nathan@postgresql.org>
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox