From 15ba58b91b0de73329f7f361c504dd52b6decd35 Mon Sep 17 00:00:00 2001 From: John Naylor Date: Tue, 23 Aug 2022 09:24:32 +0700 Subject: [PATCH 01/17] Remove empty statement Peter Smith Discussion: https://www.postgresql.org/message-id/CAHut%2BPtRGVuj8Q_GpHHxZyk7fGwdYDG8_s4GSfKoc_4Yd9vR-w%40mail.gmail.com (cherry picked from commit 1b9050da66aba821fec18376bee30d17e91e7027) --- src/backend/access/transam/xlogprefetcher.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/access/transam/xlogprefetcher.c b/src/backend/access/transam/xlogprefetcher.c index 87d14210be7..9aa56411d55 100644 --- a/src/backend/access/transam/xlogprefetcher.c +++ b/src/backend/access/transam/xlogprefetcher.c @@ -656,7 +656,7 @@ XLogPrefetcherNextBlock(uintptr_t pgsr_private, XLogRecPtr *lsn) if (!block->in_use) continue; - Assert(!BufferIsValid(block->prefetch_buffer));; + Assert(!BufferIsValid(block->prefetch_buffer)); /* * Record the LSN of this record. When it's replayed, From f79bb24919bbdfb27470b86a1657922819071edc Mon Sep 17 00:00:00 2001 From: John Naylor Date: Tue, 23 Aug 2022 09:55:05 +0700 Subject: [PATCH 02/17] Switch format specifier for replication origins to %d Using %u with uint16 causes warnings with -Wformat-signedness. There are many other warnings, but for now change only these since c920fe4818 already changed the message string for most of them. Per report from Peter Eisentraut Discussion: https://www.postgresql.org/message-id/31e63649-0355-7088-831e-b07d5f908a8c%40enterprisedb.com (cherry picked from commit ba8321349bc02423aa4d49ebb5d579ec313cf930) --- src/backend/replication/logical/origin.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/backend/replication/logical/origin.c b/src/backend/replication/logical/origin.c index 9eb97fac582..f19b72ff351 100644 --- a/src/backend/replication/logical/origin.c +++ b/src/backend/replication/logical/origin.c @@ -364,7 +364,7 @@ replorigin_drop_guts(Relation rel, RepOriginId roident, bool nowait) if (nowait) ereport(ERROR, (errcode(ERRCODE_OBJECT_IN_USE), - errmsg("could not drop replication origin with ID %u, in use by PID %d", + errmsg("could not drop replication origin with ID %d, in use by PID %d", state->roident, state->acquired_by))); @@ -408,7 +408,7 @@ replorigin_drop_guts(Relation rel, RepOriginId roident, bool nowait) */ tuple = SearchSysCache1(REPLORIGIDENT, ObjectIdGetDatum(roident)); if (!HeapTupleIsValid(tuple)) - elog(ERROR, "cache lookup failed for replication origin with ID %u", + elog(ERROR, "cache lookup failed for replication origin with ID %d", roident); CatalogTupleDelete(rel, &tuple->t_self); @@ -485,7 +485,7 @@ replorigin_by_oid(RepOriginId roident, bool missing_ok, char **roname) if (!missing_ok) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), - errmsg("replication origin with ID %u does not exist", + errmsg("replication origin with ID %d does not exist", roident))); return false; @@ -799,7 +799,7 @@ StartupReplicationOrigin(void) last_state++; ereport(LOG, - (errmsg("recovered replication state of node %u to %X/%X", + (errmsg("recovered replication state of node %d to %X/%X", disk_state.roident, LSN_FORMAT_ARGS(disk_state.remote_lsn)))); } @@ -937,7 +937,7 @@ replorigin_advance(RepOriginId node, { ereport(ERROR, (errcode(ERRCODE_OBJECT_IN_USE), - errmsg("replication origin with ID %u is already active for PID %d", + errmsg("replication origin with ID %d is already active for PID %d", replication_state->roident, replication_state->acquired_by))); } @@ -948,7 +948,7 @@ replorigin_advance(RepOriginId node, if (replication_state == NULL && free_state == NULL) ereport(ERROR, (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED), - errmsg("could not find free replication state slot for replication origin with ID %u", + errmsg("could not find free replication state slot for replication origin with ID %d", node), errhint("Increase max_replication_slots and try again."))); @@ -1126,7 +1126,7 @@ replorigin_session_setup(RepOriginId node) { ereport(ERROR, (errcode(ERRCODE_OBJECT_IN_USE), - errmsg("replication origin with ID %u is already active for PID %d", + errmsg("replication origin with ID %d is already active for PID %d", curstate->roident, curstate->acquired_by))); } @@ -1138,7 +1138,7 @@ replorigin_session_setup(RepOriginId node) if (session_replication_state == NULL && free_slot == -1) ereport(ERROR, (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED), - errmsg("could not find free replication state slot for replication origin with ID %u", + errmsg("could not find free replication state slot for replication origin with ID %d", node), errhint("Increase max_replication_slots and try again."))); else if (session_replication_state == NULL) From 3969ea41e41ef9cf0c19aa0378c168d40f72633e Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Mon, 22 Aug 2022 20:16:50 -0700 Subject: [PATCH 03/17] pgstat: Acquire lock when reading variable-numbered stats Somewhere during the development of the patch acquiring a lock during read access to variable-numbered stats got lost. The missing lock acquisition won't cause corruption, but can lead to reading torn values when accessing stats. Add the missing lock acquisitions. Reported-by: Greg Stark Reviewed-by: "Drouvot, Bertrand" Reviewed-by: Andres Freund Author: Kyotaro Horiguchi Discussion: https://postgr.es/m/CAM-w4HMYkM_DkYhWtUGV+qE_rrBxKOzOF0+5faozxO3vXrc9wA@mail.gmail.com Backpatch: 15- (cherry picked from commit cd063344fb801a90a40923a5b8aefe4eb8d80762) --- src/backend/utils/activity/pgstat.c | 9 +++++++++ src/backend/utils/activity/pgstat_shmem.c | 16 ++++++++++++++++ src/include/utils/pgstat_internal.h | 1 + 3 files changed, 26 insertions(+) diff --git a/src/backend/utils/activity/pgstat.c b/src/backend/utils/activity/pgstat.c index 4ac6a1662f7..20139a51121 100644 --- a/src/backend/utils/activity/pgstat.c +++ b/src/backend/utils/activity/pgstat.c @@ -854,9 +854,12 @@ pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, Oid objoid) else stats_data = MemoryContextAlloc(pgStatLocal.snapshot.context, kind_info->shared_data_len); + + pgstat_lock_entry_shared(entry_ref, false); memcpy(stats_data, pgstat_get_entry_data(kind, entry_ref->shared_stats), kind_info->shared_data_len); + pgstat_unlock_entry(entry_ref); if (pgstat_fetch_consistency > PGSTAT_FETCH_CONSISTENCY_NONE) { @@ -993,9 +996,15 @@ pgstat_build_snapshot(void) entry->data = MemoryContextAlloc(pgStatLocal.snapshot.context, kind_info->shared_size); + /* + * Acquire the LWLock directly instead of using + * pg_stat_lock_entry_shared() which requires a reference. + */ + LWLockAcquire(&stats_data->lock, LW_SHARED); memcpy(entry->data, pgstat_get_entry_data(kind, stats_data), kind_info->shared_size); + LWLockRelease(&stats_data->lock); } dshash_seq_term(&hstat); diff --git a/src/backend/utils/activity/pgstat_shmem.c b/src/backend/utils/activity/pgstat_shmem.c index 89060ef29a0..ac989186884 100644 --- a/src/backend/utils/activity/pgstat_shmem.c +++ b/src/backend/utils/activity/pgstat_shmem.c @@ -579,6 +579,22 @@ pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait) return true; } +/* + * Separate from pgstat_lock_entry() as most callers will need to lock + * exclusively. + */ +bool +pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait) +{ + LWLock *lock = &entry_ref->shared_stats->lock; + + if (nowait) + return LWLockConditionalAcquire(lock, LW_SHARED); + + LWLockAcquire(lock, LW_SHARED); + return true; +} + void pgstat_unlock_entry(PgStat_EntryRef *entry_ref) { diff --git a/src/include/utils/pgstat_internal.h b/src/include/utils/pgstat_internal.h index 9303d05427f..901d2041d66 100644 --- a/src/include/utils/pgstat_internal.h +++ b/src/include/utils/pgstat_internal.h @@ -581,6 +581,7 @@ extern void pgstat_detach_shmem(void); extern PgStat_EntryRef *pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, Oid objoid, bool create, bool *found); extern bool pgstat_lock_entry(PgStat_EntryRef *entry_ref, bool nowait); +extern bool pgstat_lock_entry_shared(PgStat_EntryRef *entry_ref, bool nowait); extern void pgstat_unlock_entry(PgStat_EntryRef *entry_ref); extern bool pgstat_drop_entry(PgStat_Kind kind, Oid dboid, Oid objoid); extern void pgstat_drop_all_entries(void); From efee082f659408f1b1b46ff6afd737adeb8ff4fc Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Mon, 22 Aug 2022 20:22:50 -0700 Subject: [PATCH 04/17] Add BackendType for standalone backends All backends should have a BackendType to enable statistics reporting per BackendType. Add a new BackendType for standalone backends, B_STANDALONE_BACKEND (and alphabetize the BackendTypes). Both the bootstrap backend and single user mode backends will have BackendType B_STANDALONE_BACKEND. Author: Melanie Plageman Reviewed-by: Andres Freund Discussion: https://www.postgresql.org/message-id/CAAKRu_aaq33UnG4TXq3S-OSXGWj1QGf0sU%2BECH4tNwGFNERkZA%40mail.gmail.com (cherry picked from commit 0c679464a837079acc75ff1d45eaa83f79e05690) --- src/backend/utils/init/miscinit.c | 17 +++++++++++------ src/include/miscadmin.h | 5 +++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c index e87c72186ba..852f19a1d6a 100644 --- a/src/backend/utils/init/miscinit.c +++ b/src/backend/utils/init/miscinit.c @@ -176,6 +176,8 @@ InitStandaloneProcess(const char *argv0) { Assert(!IsPostmasterEnvironment); + MyBackendType = B_STANDALONE_BACKEND; + /* * Start our win32 signal implementation */ @@ -255,6 +257,9 @@ GetBackendTypeDesc(BackendType backendType) case B_INVALID: backendDesc = "not initialized"; break; + case B_ARCHIVER: + backendDesc = "archiver"; + break; case B_AUTOVAC_LAUNCHER: backendDesc = "autovacuum launcher"; break; @@ -273,6 +278,12 @@ GetBackendTypeDesc(BackendType backendType) case B_CHECKPOINTER: backendDesc = "checkpointer"; break; + case B_LOGGER: + backendDesc = "logger"; + break; + case B_STANDALONE_BACKEND: + backendDesc = "standalone backend"; + break; case B_STARTUP: backendDesc = "startup"; break; @@ -285,12 +296,6 @@ GetBackendTypeDesc(BackendType backendType) case B_WAL_WRITER: backendDesc = "walwriter"; break; - case B_ARCHIVER: - backendDesc = "archiver"; - break; - case B_LOGGER: - backendDesc = "logger"; - break; } return backendDesc; diff --git a/src/include/miscadmin.h b/src/include/miscadmin.h index ed189cea121..8ee29d99a69 100644 --- a/src/include/miscadmin.h +++ b/src/include/miscadmin.h @@ -316,18 +316,19 @@ extern void SwitchBackToLocalLatch(void); typedef enum BackendType { B_INVALID = 0, + B_ARCHIVER, B_AUTOVAC_LAUNCHER, B_AUTOVAC_WORKER, B_BACKEND, B_BG_WORKER, B_BG_WRITER, B_CHECKPOINTER, + B_LOGGER, + B_STANDALONE_BACKEND, B_STARTUP, B_WAL_RECEIVER, B_WAL_SENDER, B_WAL_WRITER, - B_ARCHIVER, - B_LOGGER, } BackendType; extern PGDLLIMPORT BackendType MyBackendType; From 521af47b4910782e410a710cdaac059516399d7b Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Mon, 22 Aug 2022 20:25:42 -0700 Subject: [PATCH 05/17] Remove redundant call to pgstat_report_wal() pgstat_report_stat() will be called before shutdown so an explicit call to pgstat_report_wal() just before shutdown is redundant. This likely was not redundant before 5891c7a8ed8, but now it clearly is. Author: Melanie Plageman Reviewed-by: Andres Freund Discussion: https://www.postgresql.org/message-id/CAAKRu_aaq33UnG4TXq3S-OSXGWj1QGf0sU%2BECH4tNwGFNERkZA%40mail.gmail.com (cherry picked from commit 1bdd54e662d58ed49448255a004b668b56bae100) --- src/backend/postmaster/walwriter.c | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/backend/postmaster/walwriter.c b/src/backend/postmaster/walwriter.c index e926f8c27c7..beb46dcb55c 100644 --- a/src/backend/postmaster/walwriter.c +++ b/src/backend/postmaster/walwriter.c @@ -293,18 +293,7 @@ HandleWalWriterInterrupts(void) } if (ShutdownRequestPending) - { - /* - * Force reporting remaining WAL statistics at process exit. - * - * Since pgstat_report_wal is invoked with 'force' is false in main - * loop to avoid overloading the cumulative stats system, there may - * exist unreported stats counters for the WAL writer. - */ - pgstat_report_wal(true); - proc_exit(0); - } /* Perform logging of memory contexts of this process */ if (LogMemoryContextPending) From f33543e1ff9bbc26b8d01f47408b6350081d816d Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Mon, 22 Aug 2022 20:39:30 -0700 Subject: [PATCH 06/17] Don't define FRONTEND for initdb No headers requiring FRONTED to be defined are included as of af1a949109d. Since this is the last user of (contrib|frontend)_defines in Mkvcbuild.pm, remove them. Discussion: https://postgr.es/m/20220820194550.725755r6fj2ro3rx@awork3.anarazel.de (cherry picked from commit 06e3559bade1da89feb1a53d517c991fb4f15272) --- src/bin/initdb/Makefile | 2 +- src/tools/msvc/Mkvcbuild.pm | 26 ++++++++------------------ 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/src/bin/initdb/Makefile b/src/bin/initdb/Makefile index b0dd13dfbdf..6737938c3f8 100644 --- a/src/bin/initdb/Makefile +++ b/src/bin/initdb/Makefile @@ -16,7 +16,7 @@ subdir = src/bin/initdb top_builddir = ../../.. include $(top_builddir)/src/Makefile.global -override CPPFLAGS := -DFRONTEND -I$(libpq_srcdir) -I$(top_srcdir)/src/timezone $(CPPFLAGS) +override CPPFLAGS := -I$(libpq_srcdir) -I$(top_srcdir)/src/timezone $(CPPFLAGS) # Note: it's important that we link to encnames.o from libpgcommon, not # from libpq, else we have risks of version skew if we run with a libpq diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm index ee963d85f30..156428d908e 100644 --- a/src/tools/msvc/Mkvcbuild.pm +++ b/src/tools/msvc/Mkvcbuild.pm @@ -35,7 +35,6 @@ my $libpq; my @unlink_on_exit; # Set of variables for modules in contrib/ and src/test/modules/ -my $contrib_defines = {}; my @contrib_uselibpq = (); my @contrib_uselibpgport = (); my @contrib_uselibpgcommon = (); @@ -53,7 +52,6 @@ my @contrib_excludes = ( 'unsafe_tests'); # Set of variables for frontend modules -my $frontend_defines = { 'initdb' => 'FRONTEND' }; my @frontend_uselibpq = ('pg_amcheck', 'pg_ctl', 'pg_upgrade', 'pgbench', 'psql', 'initdb'); my @frontend_uselibpgport = ( @@ -1126,10 +1124,10 @@ sub AdjustContribProj { my $proj = shift; AdjustModule( - $proj, $contrib_defines, - \@contrib_uselibpq, \@contrib_uselibpgport, - \@contrib_uselibpgcommon, $contrib_extralibs, - $contrib_extrasource, $contrib_extraincludes); + $proj, \@contrib_uselibpq, + \@contrib_uselibpgport, \@contrib_uselibpgcommon, + $contrib_extralibs, $contrib_extrasource, + $contrib_extraincludes); return; } @@ -1137,17 +1135,16 @@ sub AdjustFrontendProj { my $proj = shift; AdjustModule( - $proj, $frontend_defines, - \@frontend_uselibpq, \@frontend_uselibpgport, - \@frontend_uselibpgcommon, $frontend_extralibs, - $frontend_extrasource, $frontend_extraincludes); + $proj, \@frontend_uselibpq, + \@frontend_uselibpgport, \@frontend_uselibpgcommon, + $frontend_extralibs, $frontend_extrasource, + $frontend_extraincludes); return; } sub AdjustModule { my $proj = shift; - my $module_defines = shift; my $module_uselibpq = shift; my $module_uselibpgport = shift; my $module_uselibpgcommon = shift; @@ -1156,13 +1153,6 @@ sub AdjustModule my $module_extraincludes = shift; my $n = $proj->{name}; - if ($module_defines->{$n}) - { - foreach my $d ($module_defines->{$n}) - { - $proj->AddDefine($d); - } - } if (grep { /^$n$/ } @{$module_uselibpq}) { $proj->AddIncludeDir('src\interfaces\libpq'); From 28c2ff1c00a82fd2c0a96479aa0af7450c74af0c Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Mon, 22 Aug 2022 20:39:30 -0700 Subject: [PATCH 07/17] Don't define FRONTEND for ecpg libraries Not needed anymore after 7143b3e8213. Discussion: https://postgr.es/m/20220820194550.725755r6fj2ro3rx@awork3.anarazel.de (cherry picked from commit 9352d5cf128ad9888918bba00b87ba347438fd26) --- src/interfaces/ecpg/compatlib/Makefile | 2 +- src/interfaces/ecpg/ecpglib/Makefile | 2 +- src/interfaces/ecpg/pgtypeslib/Makefile | 2 +- src/tools/msvc/Mkvcbuild.pm | 3 --- 4 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/interfaces/ecpg/compatlib/Makefile b/src/interfaces/ecpg/compatlib/Makefile index 867ad55539a..62b8a8c12cc 100644 --- a/src/interfaces/ecpg/compatlib/Makefile +++ b/src/interfaces/ecpg/compatlib/Makefile @@ -19,7 +19,7 @@ SO_MAJOR_VERSION= 3 SO_MINOR_VERSION= $(MAJORVERSION) override CPPFLAGS := -I../include -I$(top_srcdir)/src/interfaces/ecpg/include \ - -I$(libpq_srcdir) -DFRONTEND $(CPPFLAGS) + -I$(libpq_srcdir) $(CPPFLAGS) override CFLAGS += $(PTHREAD_CFLAGS) SHLIB_LINK_INTERNAL = -L../ecpglib -lecpg -L../pgtypeslib -lpgtypes $(libpq_pgport_shlib) diff --git a/src/interfaces/ecpg/ecpglib/Makefile b/src/interfaces/ecpg/ecpglib/Makefile index b00229f3fec..844fc6eb6ec 100644 --- a/src/interfaces/ecpg/ecpglib/Makefile +++ b/src/interfaces/ecpg/ecpglib/Makefile @@ -19,7 +19,7 @@ SO_MAJOR_VERSION= 6 SO_MINOR_VERSION= $(MAJORVERSION) override CPPFLAGS := -I../include -I$(top_srcdir)/src/interfaces/ecpg/include \ - -I$(libpq_srcdir) -I$(top_builddir)/src/port -DFRONTEND $(CPPFLAGS) + -I$(libpq_srcdir) -I$(top_builddir)/src/port $(CPPFLAGS) override CFLAGS += $(PTHREAD_CFLAGS) OBJS = \ diff --git a/src/interfaces/ecpg/pgtypeslib/Makefile b/src/interfaces/ecpg/pgtypeslib/Makefile index 763787f22da..13ef992ee72 100644 --- a/src/interfaces/ecpg/pgtypeslib/Makefile +++ b/src/interfaces/ecpg/pgtypeslib/Makefile @@ -19,7 +19,7 @@ SO_MAJOR_VERSION= 3 SO_MINOR_VERSION= $(MAJORVERSION) override CPPFLAGS := -I../include -I$(top_srcdir)/src/interfaces/ecpg/include \ - -DFRONTEND $(CPPFLAGS) + $(CPPFLAGS) override CFLAGS += $(PTHREAD_CFLAGS) SHLIB_LINK_INTERNAL = -lpgcommon_shlib -lpgport_shlib diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm index 156428d908e..9d41f9aef86 100644 --- a/src/tools/msvc/Mkvcbuild.pm +++ b/src/tools/msvc/Mkvcbuild.pm @@ -315,14 +315,12 @@ sub mkvcbuild my $pgtypes = $solution->AddProject( 'libpgtypes', 'dll', 'interfaces', 'src/interfaces/ecpg/pgtypeslib'); - $pgtypes->AddDefine('FRONTEND'); $pgtypes->AddReference($libpgcommon, $libpgport); $pgtypes->UseDef('src/interfaces/ecpg/pgtypeslib/pgtypeslib.def'); $pgtypes->AddIncludeDir('src/interfaces/ecpg/include'); my $libecpg = $solution->AddProject('libecpg', 'dll', 'interfaces', 'src/interfaces/ecpg/ecpglib'); - $libecpg->AddDefine('FRONTEND'); $libecpg->AddIncludeDir('src/interfaces/ecpg/include'); $libecpg->AddIncludeDir('src/interfaces/libpq'); $libecpg->AddIncludeDir('src/port'); @@ -333,7 +331,6 @@ sub mkvcbuild my $libecpgcompat = $solution->AddProject( 'libecpg_compat', 'dll', 'interfaces', 'src/interfaces/ecpg/compatlib'); - $libecpgcompat->AddDefine('FRONTEND'); $libecpgcompat->AddIncludeDir('src/interfaces/ecpg/include'); $libecpgcompat->AddIncludeDir('src/interfaces/libpq'); $libecpgcompat->UseDef('src/interfaces/ecpg/compatlib/compatlib.def'); From cd9190fa0042eb22b8b1e4c0f36ef219df1669ab Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Mon, 22 Aug 2022 20:39:30 -0700 Subject: [PATCH 08/17] Don't define FRONTEND for libpq Not needed anymore after 7143b3e8213. Discussion: https://postgr.es/m/20220820194550.725755r6fj2ro3rx@awork3.anarazel.de (cherry picked from commit 1d77afefbd1dd13a0ebec9d6675dd305e86c0538) --- src/interfaces/libpq/Makefile | 2 +- src/tools/msvc/Mkvcbuild.pm | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/interfaces/libpq/Makefile b/src/interfaces/libpq/Makefile index 79c574eeb8b..1d31b256fc9 100644 --- a/src/interfaces/libpq/Makefile +++ b/src/interfaces/libpq/Makefile @@ -22,7 +22,7 @@ NAME= pq SO_MAJOR_VERSION= 5 SO_MINOR_VERSION= $(MAJORVERSION) -override CPPFLAGS := -DFRONTEND -I$(srcdir) $(CPPFLAGS) -I$(top_builddir)/src/port -I$(top_srcdir)/src/port +override CPPFLAGS := -I$(srcdir) $(CPPFLAGS) -I$(top_builddir)/src/port -I$(top_srcdir)/src/port ifneq ($(PORTNAME), win32) override CFLAGS += $(PTHREAD_CFLAGS) endif diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm index 9d41f9aef86..c3cef625ad3 100644 --- a/src/tools/msvc/Mkvcbuild.pm +++ b/src/tools/msvc/Mkvcbuild.pm @@ -263,7 +263,6 @@ sub mkvcbuild $libpq = $solution->AddProject('libpq', 'dll', 'interfaces', 'src/interfaces/libpq'); - $libpq->AddDefine('FRONTEND'); $libpq->AddIncludeDir('src/port'); $libpq->AddLibrary('secur32.lib'); $libpq->AddLibrary('ws2_32.lib'); From d2fc76b0422e942c022b80c2f690093387e5f51c Mon Sep 17 00:00:00 2001 From: Amit Kapila Date: Tue, 23 Aug 2022 10:20:02 +0530 Subject: [PATCH 09/17] Add CHECK_FOR_INTERRUPTS while decoding changes. While decoding changes in a loop, if we skip all the changes there is no CFI making the loop uninterruptible. Reported-by: Whale Song and Andrey Borodin Bug: 17580 Author: Masahiko Sawada Reviwed-by: Amit Kapila Backpatch-through: 10 Discussion: https://postgr.es/m/17580-849c1d5b6d7eb422@postgresql.org Discussion: https://postgr.es/m/B319ECD6-9A28-4CDF-A8F4-3591E0BF2369@yandex-team.ru (cherry picked from commit f972ec5c285c3bc50d81f8044e08cd636017ab68) --- src/backend/replication/logical/reorderbuffer.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c index 1c21a1d14b6..89cf9f9389c 100644 --- a/src/backend/replication/logical/reorderbuffer.c +++ b/src/backend/replication/logical/reorderbuffer.c @@ -2096,6 +2096,8 @@ ReorderBufferProcessTXN(ReorderBuffer *rb, ReorderBufferTXN *txn, Relation relation = NULL; Oid reloid; + CHECK_FOR_INTERRUPTS(); + /* * We can't call start stream callback before processing first * change. From 0e867026dd356c1997cfee9785bb4dd21afaab48 Mon Sep 17 00:00:00 2001 From: Thomas Munro Date: Tue, 23 Aug 2022 23:52:17 +1200 Subject: [PATCH 10/17] Don't bother to set sockaddr_un.sun_len. It's not necessary to fill in sun_len when calling bind() or connect(), on all known systems that have it. Discussion: https://postgr.es/m/2781112.1644819528%40sss.pgh.pa.us (cherry picked from commit c9818798147a4eec00bba61a05fa9bc88398ec3b) --- src/common/ip.c | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/common/ip.c b/src/common/ip.c index 9b611cdc8c3..6343f49a70a 100644 --- a/src/common/ip.c +++ b/src/common/ip.c @@ -218,20 +218,6 @@ getaddrinfo_unix(const char *path, const struct addrinfo *hintsp, aip->ai_addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(path); } - /* - * The standard recommendation for filling sun_len is to set it to the - * struct size (independently of the actual path length). However, that - * draws an integer-overflow warning on AIX 7.1, where sun_len is just - * uint8 yet the struct size exceeds 255 bytes. It's likely that nothing - * is paying attention to sun_len on that platform, but we have to do - * something with it. To suppress the warning, clamp the struct size to - * what will fit in sun_len. - */ -#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN - unp->sun_len = Min(sizeof(struct sockaddr_un), - ((size_t) 1 << (sizeof(unp->sun_len) * BITS_PER_BYTE)) - 1); -#endif - return 0; } From 19b53c33939dd1f5c7fc14f8126bd307f271afee Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Tue, 23 Aug 2022 15:39:36 +0200 Subject: [PATCH 11/17] Remove offsetof definition This was only needed to deal with some ancient and no longer supported systems. Reviewed-by: Tom Lane Discussion: https://www.postgresql.org/message-id/flat/9a5223a2-3e25-d4fb-f092-01ec17428584%40enterprisedb.com (cherry picked from commit bd67b7e01031b688f2f19f657ece4aa2322dbcc8) --- src/include/c.h | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/src/include/c.h b/src/include/c.h index dfc366b026f..a381f9a6c40 100644 --- a/src/include/c.h +++ b/src/include/c.h @@ -30,7 +30,7 @@ * 2) bool, true, false * 3) standard system types * 4) IsValid macros for system types - * 5) offsetof, lengthof, alignment + * 5) lengthof, alignment * 6) assertions * 7) widely useful macros * 8) random stuff @@ -703,20 +703,9 @@ typedef NameData *Name; /* ---------------------------------------------------------------- - * Section 5: offsetof, lengthof, alignment + * Section 5: lengthof, alignment * ---------------------------------------------------------------- */ -/* - * offsetof - * Offset of a structure/union field within that structure/union. - * - * XXX This is supposed to be part of stddef.h, but isn't on - * some systems (like SunOS 4). - */ -#ifndef offsetof -#define offsetof(type, field) ((long) &((type *)0)->field) -#endif /* offsetof */ - /* * lengthof * Number of elements in an array. From f80f855c01b6270ce44b912168eefe9c07411b0e Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 23 Aug 2022 09:41:37 -0400 Subject: [PATCH 12/17] Doc: prefer sysctl to /proc/sys in docs and comments. sysctl is more portable than Linux's /proc/sys file tree, and often easier to use too. That's why most of our docs refer to sysctl when talking about how to adjust kernel parameters. Bring the few stragglers into line. Discussion: https://postgr.es/m/361175.1661187463@sss.pgh.pa.us (cherry picked from commit 4ee6740167b6a311660a1e8752447a496dd0d235) --- doc/src/sgml/runtime.sgml | 11 ++++++----- src/backend/postmaster/postmaster.c | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/doc/src/sgml/runtime.sgml b/doc/src/sgml/runtime.sgml index 963b18ed850..5d0b32121d9 100644 --- a/doc/src/sgml/runtime.sgml +++ b/doc/src/sgml/runtime.sgml @@ -1258,11 +1258,12 @@ default:\ - On Linux - /proc/sys/fs/file-max determines the - maximum number of open files that the kernel will support. It can - be changed by writing a different number into the file or by - adding an assignment in /etc/sysctl.conf. + On Linux the kernel parameter + fs.file-max determines the maximum number of open + files that the kernel will support. It can be changed with + sysctl -w fs.file-max=N. + To make the setting persist across reboots, add an assignment + in /etc/sysctl.conf. The maximum limit of files per process is fixed at the time the kernel is compiled; see /usr/src/linux/Documentation/proc.txt for diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index fb10e122d3e..c172a0642c2 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -5065,7 +5065,7 @@ SubPostmasterMain(int argc, char *argv[]) * If testing EXEC_BACKEND on Linux, you should run this as root before * starting the postmaster: * - * echo 0 >/proc/sys/kernel/randomize_va_space + * sysctl -w kernel.randomize_va_space=0 * * This prevents using randomized stack and code addresses that cause the * child process's memory map to be different from the parent's, making it From 58aa237aea52a656877076261b8809a84e282591 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 23 Aug 2022 09:55:37 -0400 Subject: [PATCH 13/17] Doc: document possible need to raise kernel's somaxconn limit. On fast machines, it's possible for applications such as pgbench to issue connection requests so quickly that the postmaster's listen queue overflows in the kernel, resulting in unexpected failures (with not-very-helpful error messages). Most modern OSes allow the queue size to be increased, so document how to do that. Per report from Kevin McKibbin. Discussion: https://postgr.es/m/CADc_NKg2d+oZY9mg4DdQdoUcGzN2kOYXBu-3--RW_hEe0tUV=g@mail.gmail.com (cherry picked from commit ba94dfd4c4f63cf8663f16e427bcc6fac35a4428) --- doc/src/sgml/runtime.sgml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/doc/src/sgml/runtime.sgml b/doc/src/sgml/runtime.sgml index 5d0b32121d9..c802c0c5f50 100644 --- a/doc/src/sgml/runtime.sgml +++ b/doc/src/sgml/runtime.sgml @@ -1299,6 +1299,22 @@ default:\ linkend="guc-max-files-per-process"/> configuration parameter to limit the consumption of open files. + + + Another kernel limit that may be of concern when supporting large + numbers of client connections is the maximum socket connection queue + length. If more than that many connection requests arrive within a very + short period, some may get rejected before the postmaster can service + the requests, with those clients receiving unhelpful connection failure + errors such as Resource temporarily unavailable or + Connection refused. The default queue length limit is 128 + on many platforms. To raise it, adjust the appropriate kernel parameter + via sysctl, then restart the postmaster. + The parameter is variously named net.core.somaxconn + on Linux, kern.ipc.soacceptqueue on newer FreeBSD, + and kern.ipc.somaxconn on macOS and other BSD + variants. + From 7840d045c5dae63750efc5f651f54d7a812040dd Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 23 Aug 2022 10:14:45 -0400 Subject: [PATCH 14/17] Remove our artificial PG_SOMAXCONN limit on listen queue length. I added this in commit 153f40067, out of paranoia about kernels possibly rejecting very large listen backlog requests. However, POSIX has said for decades that the kernel must silently reduce any value it considers too large, and there's no evidence that any current system doesn't obey that. Let's just drop this limit and save some complication. While we're here, compute the request as twice MaxConnections not twice MaxBackends; the latter no longer means what it did in 2001. Per discussion of a report from Kevin McKibbin. Discussion: https://postgr.es/m/CADc_NKg2d+oZY9mg4DdQdoUcGzN2kOYXBu-3--RW_hEe0tUV=g@mail.gmail.com (cherry picked from commit 0f47457f1129fa21131465aba5663a14b2a09386) --- src/backend/libpq/pqcomm.c | 10 ++++------ src/include/pg_config_manual.h | 11 ----------- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/src/backend/libpq/pqcomm.c b/src/backend/libpq/pqcomm.c index 3287b1c7742..27965cd690c 100644 --- a/src/backend/libpq/pqcomm.c +++ b/src/backend/libpq/pqcomm.c @@ -533,13 +533,11 @@ StreamServerPort(int family, const char *hostName, unsigned short portNumber, } /* - * Select appropriate accept-queue length limit. PG_SOMAXCONN is only - * intended to provide a clamp on the request on platforms where an - * overly large request provokes a kernel error (are there any?). + * Select appropriate accept-queue length limit. It seems reasonable + * to use a value similar to the maximum number of child processes + * that the postmaster will permit. */ - maxconn = MaxBackends * 2; - if (maxconn > PG_SOMAXCONN) - maxconn = PG_SOMAXCONN; + maxconn = MaxConnections * 2; err = listen(fd, maxconn); if (err < 0) diff --git a/src/include/pg_config_manual.h b/src/include/pg_config_manual.h index 844c3e0f094..f2a106f983d 100644 --- a/src/include/pg_config_manual.h +++ b/src/include/pg_config_manual.h @@ -114,17 +114,6 @@ */ #define MAXPGPATH 1024 -/* - * PG_SOMAXCONN: maximum accept-queue length limit passed to - * listen(2). You'd think we should use SOMAXCONN from - * , but on many systems that symbol is much smaller - * than the kernel's actual limit. In any case, this symbol need be - * twiddled only if you have a kernel that refuses large limit values, - * rather than silently reducing the value to what it can handle - * (which is what most if not all Unixen do). - */ -#define PG_SOMAXCONN 10000 - /* * You can try changing this if you have a machine with bytes of * another size, but no guarantee... From 448cf83e88532292bce8c7cbd8a109163b4a4d33 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Tue, 23 Aug 2022 16:00:38 +0200 Subject: [PATCH 15/17] Remove further unwanted linker flags from perl_embed_ldflags Remove the contents of $Config{ldflags} from ExtUtils::Embed's ldopts, like we already do with $Config{ccdlflags}. Those flags are the choices of those who built the Perl installation, which are not necessarily appropriate for building PostgreSQL. What we really want from ldopts are the options identifying the location and name of the libperl library, but unfortunately it doesn't appear possible to get that separately from the other stuff. The motivation for this was to strip -mmacosx-version-min options. We already did something similar for the -arch option. Both of those are now covered by this more general approach. Reviewed-by: Tom Lane Reviewed-by: Andres Freund Discussion: https://www.postgresql.org/message-id/flat/8c4fcb72-2574-ff7c-4c25-1f032d4a2a57%40enterprisedb.com (cherry picked from commit b4e936859dc441102eb0b6fb7a104f3948c90490) --- config/perl.m4 | 10 +++++----- configure | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/config/perl.m4 b/config/perl.m4 index c823fc8cf07..c9fd91397c2 100644 --- a/config/perl.m4 +++ b/config/perl.m4 @@ -81,9 +81,9 @@ AC_MSG_RESULT([$perl_embed_ccflags]) # PGAC_CHECK_PERL_EMBED_LDFLAGS # ----------------------------- # We are after Embed's ldopts, but without the subset mentioned in -# Config's ccdlflags; and also without any -arch flags, which recent -# Apple releases put in unhelpfully. (If you want a multiarch build -# you'd better be specifying it in more places than plperl's final link.) +# Config's ccdlflags and ldflags. (Those are the choices of those who +# built the Perl installation, which are not necessarily appropriate +# for building PostgreSQL.) AC_DEFUN([PGAC_CHECK_PERL_EMBED_LDFLAGS], [AC_REQUIRE([PGAC_PATH_PERL]) AC_MSG_CHECKING(for flags to link embedded Perl) @@ -99,8 +99,8 @@ if test "$PORTNAME" = "win32" ; then fi else pgac_tmp1=`$PERL -MExtUtils::Embed -e ldopts` - pgac_tmp2=`$PERL -MConfig -e 'print $Config{ccdlflags}'` - perl_embed_ldflags=`echo X"$pgac_tmp1" | sed -e "s/^X//" -e "s%$pgac_tmp2%%" -e ["s/ -arch [-a-zA-Z0-9_]*//g"]` + pgac_tmp2=`$PERL -MConfig -e 'print "$Config{ccdlflags} $Config{ldflags}"'` + perl_embed_ldflags=`echo X"$pgac_tmp1" | sed -e "s/^X//" -e "s%$pgac_tmp2%%"` fi AC_SUBST(perl_embed_ldflags)dnl if test -z "$perl_embed_ldflags" ; then diff --git a/configure b/configure index 4bd050008ba..814014a96b1 100755 --- a/configure +++ b/configure @@ -10477,8 +10477,8 @@ if test "$PORTNAME" = "win32" ; then fi else pgac_tmp1=`$PERL -MExtUtils::Embed -e ldopts` - pgac_tmp2=`$PERL -MConfig -e 'print $Config{ccdlflags}'` - perl_embed_ldflags=`echo X"$pgac_tmp1" | sed -e "s/^X//" -e "s%$pgac_tmp2%%" -e "s/ -arch [-a-zA-Z0-9_]*//g"` + pgac_tmp2=`$PERL -MConfig -e 'print "$Config{ccdlflags} $Config{ldflags}"'` + perl_embed_ldflags=`echo X"$pgac_tmp1" | sed -e "s/^X//" -e "s%$pgac_tmp2%%"` fi if test -z "$perl_embed_ldflags" ; then { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5 From 7bcde56457715e9612682089c15c1396baa9e15d Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Tue, 23 Aug 2022 21:50:12 +0200 Subject: [PATCH 16/17] Message style adjustment (cherry picked from commit 869e56a39976a42a393adc2d69df3abce7eff18f) --- src/interfaces/libpq/fe-auth-scram.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/interfaces/libpq/fe-auth-scram.c b/src/interfaces/libpq/fe-auth-scram.c index 5012806fa5e..396b4ca0f0f 100644 --- a/src/interfaces/libpq/fe-auth-scram.c +++ b/src/interfaces/libpq/fe-auth-scram.c @@ -931,7 +931,7 @@ pg_fe_scram_build_secret(const char *password, const char **errstr) /* Generate a random salt */ if (!pg_strong_random(saltbuf, SCRAM_DEFAULT_SALT_LEN)) { - *errstr = _("failed to generate random salt"); + *errstr = _("could not generate random salt"); free(prep_password); return NULL; } From 966a091c804ac2904e30f03df3c2362fb5eae9a8 Mon Sep 17 00:00:00 2001 From: David Rowley Date: Wed, 24 Aug 2022 12:27:12 +1200 Subject: [PATCH 17/17] Further reduce warnings with -Wshadow=compatible-local In a similar effort to f01592f91, here we're targetting fixing the warnings that -Wshadow=compatible-local produces that we can fix by moving a variable to an inner scope to stop that variable from being shadowed by another variable declared somewhere later in the function. All of the warnings being fixed here are changing the scope of variables which are being used as an iterator for a "for" loop. In each instance, the fix happens to be changing the for loop to use the C99 type initialization. Much of this code likely pre-dates our use of C99. Reducing the scope of the outer scoped variable seems like the safest way to fix these. Renaming seems more likely to risk patches using the wrong variable. Reducing the scope is more likely to result in a compilation failure after applying some future patch rather than introducing bugs with it. By my count, this takes the warning count from 129 down to 114. Author: Justin Pryzby Discussion: https://postgr.es/m/CAApHDvrwLGBP%2BYw9vriayyf%3DXR4uPWP5jr6cQhP9au_kaDUhbA%40mail.gmail.com (cherry picked from commit 421892a192b8f95ab96c5edb61d424f80a4221d0) --- src/backend/access/brin/brin.c | 3 +-- src/backend/access/brin/brin_minmax_multi.c | 3 +-- src/backend/access/gist/gist.c | 3 +-- src/backend/commands/copyfrom.c | 3 +-- src/backend/commands/indexcmds.c | 7 +++---- src/backend/executor/nodeAgg.c | 3 +-- src/backend/optimizer/path/costsize.c | 8 ++++---- src/backend/statistics/mcv.c | 11 +++++------ src/backend/storage/buffer/bufmgr.c | 5 ++--- src/bin/pg_dump/pg_dump.c | 3 +-- src/interfaces/ecpg/pgtypeslib/numeric.c | 5 ++--- 11 files changed, 22 insertions(+), 32 deletions(-) diff --git a/src/backend/access/brin/brin.c b/src/backend/access/brin/brin.c index e88f7efa7e4..69f21abfb59 100644 --- a/src/backend/access/brin/brin.c +++ b/src/backend/access/brin/brin.c @@ -372,7 +372,6 @@ bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm) **nullkeys; int *nkeys, *nnullkeys; - int keyno; char *ptr; Size len; char *tmp PG_USED_FOR_ASSERTS_ONLY; @@ -454,7 +453,7 @@ bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm) memset(nnullkeys, 0, sizeof(int) * bdesc->bd_tupdesc->natts); /* Preprocess the scan keys - split them into per-attribute arrays. */ - for (keyno = 0; keyno < scan->numberOfKeys; keyno++) + for (int keyno = 0; keyno < scan->numberOfKeys; keyno++) { ScanKey key = &scan->keyData[keyno]; AttrNumber keyattno = key->sk_attno; diff --git a/src/backend/access/brin/brin_minmax_multi.c b/src/backend/access/brin/brin_minmax_multi.c index 10d4f17bc6f..a581659fe2b 100644 --- a/src/backend/access/brin/brin_minmax_multi.c +++ b/src/backend/access/brin/brin_minmax_multi.c @@ -582,7 +582,6 @@ brin_range_serialize(Ranges *range) int typlen; bool typbyval; - int i; char *ptr; /* simple sanity checks */ @@ -662,7 +661,7 @@ brin_range_serialize(Ranges *range) */ ptr = serialized->data; /* start of the serialized data */ - for (i = 0; i < nvalues; i++) + for (int i = 0; i < nvalues; i++) { if (typbyval) /* simple by-value data types */ { diff --git a/src/backend/access/gist/gist.c b/src/backend/access/gist/gist.c index 5866c6aaaf7..30069f139c7 100644 --- a/src/backend/access/gist/gist.c +++ b/src/backend/access/gist/gist.c @@ -234,7 +234,6 @@ gistplacetopage(Relation rel, Size freespace, GISTSTATE *giststate, Page page = BufferGetPage(buffer); bool is_leaf = (GistPageIsLeaf(page)) ? true : false; XLogRecPtr recptr; - int i; bool is_split; /* @@ -420,7 +419,7 @@ gistplacetopage(Relation rel, Size freespace, GISTSTATE *giststate, { char *data = (char *) (ptr->list); - for (i = 0; i < ptr->block.num; i++) + for (int i = 0; i < ptr->block.num; i++) { IndexTuple thistup = (IndexTuple) data; diff --git a/src/backend/commands/copyfrom.c b/src/backend/commands/copyfrom.c index a976008b3d4..e8bb168aea8 100644 --- a/src/backend/commands/copyfrom.c +++ b/src/backend/commands/copyfrom.c @@ -1202,7 +1202,6 @@ BeginCopyFrom(ParseState *pstate, num_defaults; FmgrInfo *in_functions; Oid *typioparams; - int attnum; Oid in_func_oid; int *defmap; ExprState **defexprs; @@ -1401,7 +1400,7 @@ BeginCopyFrom(ParseState *pstate, defmap = (int *) palloc(num_phys_attrs * sizeof(int)); defexprs = (ExprState **) palloc(num_phys_attrs * sizeof(ExprState *)); - for (attnum = 1; attnum <= num_phys_attrs; attnum++) + for (int attnum = 1; attnum <= num_phys_attrs; attnum++) { Form_pg_attribute att = TupleDescAttr(tupDesc, attnum - 1); diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index 667f2a4cd16..3c6e09815e0 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -565,7 +565,6 @@ DefineIndex(Oid relationId, Oid root_save_userid; int root_save_sec_context; int root_save_nestlevel; - int i; root_save_nestlevel = NewGUCNestLevel(); @@ -1047,7 +1046,7 @@ DefineIndex(Oid relationId, * We disallow indexes on system columns. They would not necessarily get * updated correctly, and they don't seem useful anyway. */ - for (i = 0; i < indexInfo->ii_NumIndexAttrs; i++) + for (int i = 0; i < indexInfo->ii_NumIndexAttrs; i++) { AttrNumber attno = indexInfo->ii_IndexAttrNumbers[i]; @@ -1067,7 +1066,7 @@ DefineIndex(Oid relationId, pull_varattnos((Node *) indexInfo->ii_Expressions, 1, &indexattrs); pull_varattnos((Node *) indexInfo->ii_Predicate, 1, &indexattrs); - for (i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++) + for (int i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++) { if (bms_is_member(i - FirstLowInvalidHeapAttributeNumber, indexattrs)) @@ -1243,7 +1242,7 @@ DefineIndex(Oid relationId, * If none matches, build a new index by calling ourselves * recursively with the same options (except for the index name). */ - for (i = 0; i < nparts; i++) + for (int i = 0; i < nparts; i++) { Oid childRelid = part_oids[i]; Relation childrel; diff --git a/src/backend/executor/nodeAgg.c b/src/backend/executor/nodeAgg.c index 96d200e4461..933c3049016 100644 --- a/src/backend/executor/nodeAgg.c +++ b/src/backend/executor/nodeAgg.c @@ -1296,13 +1296,12 @@ finalize_aggregates(AggState *aggstate, Datum *aggvalues = econtext->ecxt_aggvalues; bool *aggnulls = econtext->ecxt_aggnulls; int aggno; - int transno; /* * If there were any DISTINCT and/or ORDER BY aggregates, sort their * inputs and run the transition functions. */ - for (transno = 0; transno < aggstate->numtrans; transno++) + for (int transno = 0; transno < aggstate->numtrans; transno++) { AggStatePerTrans pertrans = &aggstate->pertrans[transno]; AggStatePerGroup pergroupstate; diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c index 1e94c5aa7c4..75acea149c7 100644 --- a/src/backend/optimizer/path/costsize.c +++ b/src/backend/optimizer/path/costsize.c @@ -2447,7 +2447,6 @@ append_nonpartial_cost(List *subpaths, int numpaths, int parallel_workers) int arrlen; ListCell *l; ListCell *cell; - int i; int path_index; int min_index; int max_index; @@ -2486,7 +2485,6 @@ append_nonpartial_cost(List *subpaths, int numpaths, int parallel_workers) for_each_cell(l, subpaths, cell) { Path *subpath = (Path *) lfirst(l); - int i; /* Consider only the non-partial paths */ if (path_index++ == numpaths) @@ -2495,7 +2493,8 @@ append_nonpartial_cost(List *subpaths, int numpaths, int parallel_workers) costarr[min_index] += subpath->total_cost; /* Update the new min cost array index */ - for (min_index = i = 0; i < arrlen; i++) + min_index = 0; + for (int i = 0; i < arrlen; i++) { if (costarr[i] < costarr[min_index]) min_index = i; @@ -2503,7 +2502,8 @@ append_nonpartial_cost(List *subpaths, int numpaths, int parallel_workers) } /* Return the highest cost from the array */ - for (max_index = i = 0; i < arrlen; i++) + max_index = 0; + for (int i = 0; i < arrlen; i++) { if (costarr[i] > costarr[max_index]) max_index = i; diff --git a/src/backend/statistics/mcv.c b/src/backend/statistics/mcv.c index 5410a68bc91..6eeacb0d476 100644 --- a/src/backend/statistics/mcv.c +++ b/src/backend/statistics/mcv.c @@ -1604,7 +1604,6 @@ mcv_get_match_bitmap(PlannerInfo *root, List *clauses, Bitmapset *keys, List *exprs, MCVList *mcvlist, bool is_or) { - int i; ListCell *l; bool *matches; @@ -1659,7 +1658,7 @@ mcv_get_match_bitmap(PlannerInfo *root, List *clauses, * can skip items that were already ruled out, and terminate if * there are no remaining MCV items that might possibly match. */ - for (i = 0; i < mcvlist->nitems; i++) + for (int i = 0; i < mcvlist->nitems; i++) { bool match = true; MCVItem *item = &mcvlist->items[i]; @@ -1766,7 +1765,7 @@ mcv_get_match_bitmap(PlannerInfo *root, List *clauses, * can skip items that were already ruled out, and terminate if * there are no remaining MCV items that might possibly match. */ - for (i = 0; i < mcvlist->nitems; i++) + for (int i = 0; i < mcvlist->nitems; i++) { int j; bool match = !expr->useOr; @@ -1837,7 +1836,7 @@ mcv_get_match_bitmap(PlannerInfo *root, List *clauses, * can skip items that were already ruled out, and terminate if * there are no remaining MCV items that might possibly match. */ - for (i = 0; i < mcvlist->nitems; i++) + for (int i = 0; i < mcvlist->nitems; i++) { bool match = false; /* assume mismatch */ MCVItem *item = &mcvlist->items[i]; @@ -1930,7 +1929,7 @@ mcv_get_match_bitmap(PlannerInfo *root, List *clauses, * can skip items that were already ruled out, and terminate if * there are no remaining MCV items that might possibly match. */ - for (i = 0; i < mcvlist->nitems; i++) + for (int i = 0; i < mcvlist->nitems; i++) { MCVItem *item = &mcvlist->items[i]; bool match = false; @@ -1956,7 +1955,7 @@ mcv_get_match_bitmap(PlannerInfo *root, List *clauses, * can skip items that were already ruled out, and terminate if * there are no remaining MCV items that might possibly match. */ - for (i = 0; i < mcvlist->nitems; i++) + for (int i = 0; i < mcvlist->nitems; i++) { bool match; MCVItem *item = &mcvlist->items[i]; diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 7a1202c6096..49d3b8c9dd0 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -3183,7 +3183,6 @@ void DropRelationsAllBuffers(SMgrRelation *smgr_reln, int nlocators) { int i; - int j; int n = 0; SMgrRelation *rels; BlockNumber (*block)[MAX_FORKNUM + 1]; @@ -3232,7 +3231,7 @@ DropRelationsAllBuffers(SMgrRelation *smgr_reln, int nlocators) */ for (i = 0; i < n && cached; i++) { - for (j = 0; j <= MAX_FORKNUM; j++) + for (int j = 0; j <= MAX_FORKNUM; j++) { /* Get the number of blocks for a relation's fork. */ block[i][j] = smgrnblocks_cached(rels[i], j); @@ -3259,7 +3258,7 @@ DropRelationsAllBuffers(SMgrRelation *smgr_reln, int nlocators) { for (i = 0; i < n; i++) { - for (j = 0; j <= MAX_FORKNUM; j++) + for (int j = 0; j <= MAX_FORKNUM; j++) { /* ignore relation forks that doesn't exist */ if (!BlockNumberIsValid(block[i][j])) diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 89eb56411fb..79fb0b7245b 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -11665,7 +11665,6 @@ dumpFunc(Archive *fout, const FuncInfo *finfo) char **configitems = NULL; int nconfigitems = 0; const char *keyword; - int i; /* Do nothing in data-only dump */ if (dopt->dataOnly) @@ -11945,7 +11944,7 @@ dumpFunc(Archive *fout, const FuncInfo *finfo) finfo->dobj.name); } - for (i = 0; i < nconfigitems; i++) + for (int i = 0; i < nconfigitems; i++) { /* we feel free to scribble on configitems[] here */ char *configitem = configitems[i]; diff --git a/src/interfaces/ecpg/pgtypeslib/numeric.c b/src/interfaces/ecpg/pgtypeslib/numeric.c index a97b3300cb8..35e7b92da40 100644 --- a/src/interfaces/ecpg/pgtypeslib/numeric.c +++ b/src/interfaces/ecpg/pgtypeslib/numeric.c @@ -1062,7 +1062,6 @@ PGTYPESnumeric_div(numeric *var1, numeric *var2, numeric *result) int weight_tmp; int rscale_tmp; int ri; - int i; long guess; long first_have; long first_div; @@ -1109,7 +1108,7 @@ PGTYPESnumeric_div(numeric *var1, numeric *var2, numeric *result) * Initialize local variables */ init_var(÷nd); - for (i = 1; i < 10; i++) + for (int i = 1; i < 10; i++) init_var(&divisor[i]); /* @@ -1268,7 +1267,7 @@ PGTYPESnumeric_div(numeric *var1, numeric *var2, numeric *result) if (dividend.buf != NULL) digitbuf_free(dividend.buf); - for (i = 1; i < 10; i++) + for (int i = 1; i < 10; i++) { if (divisor[i].buf != NULL) digitbuf_free(divisor[i].buf);