From 1803800043ef730a891c2d6928f7133a95cf7970 Mon Sep 17 00:00:00 2001
From: Tristan Partin <tristan@partin.io>
Date: Wed, 23 Sep 2026 02:59:33 +0000
Subject: [PATCH v2 2/8] Add pg_attribute_counted_by() to various flexible
 array members

Annotate flexible array members whose element count is held by another
member of the same struct, so that the compiler knows the real extent of
these arrays.

Author: Tristan Partin <tristan@partin.io>
Signed-off-by: Tristan Partin <tristan@partin.io>
---
 contrib/hstore/hstore_io.c                     |  2 +-
 contrib/pageinspect/brinfuncs.c                |  2 +-
 src/backend/access/brin/brin_minmax_multi.c    |  2 +-
 src/backend/access/nbtree/nbtutils.c           |  2 +-
 src/backend/access/transam/multixact.c         |  2 +-
 src/backend/access/transam/xact.c              |  2 +-
 src/backend/access/transam/xlogprefetcher.c    |  2 +-
 src/backend/catalog/index.c                    |  2 +-
 src/backend/commands/tablespace.c              |  2 +-
 src/backend/commands/trigger.c                 |  2 +-
 src/backend/executor/execParallel.c            |  2 +-
 src/backend/nodes/bitmapset.c                  |  4 ++--
 src/backend/postmaster/checkpointer.c          |  2 +-
 src/backend/postmaster/launch_backend.c        |  2 +-
 .../replication/logical/reorderbuffer.c        |  2 +-
 src/backend/storage/buffer/freelist.c          |  2 +-
 src/backend/storage/ipc/dsm.c                  |  2 +-
 src/backend/storage/ipc/pmsignal.c             |  2 +-
 src/backend/utils/adt/jsonfuncs.c              |  2 +-
 src/backend/utils/adt/rowtypes.c               |  4 ++--
 src/backend/utils/adt/tsvector_op.c            |  2 +-
 src/backend/utils/cache/typcache.c             |  2 +-
 src/backend/utils/sort/sharedtuplestore.c      |  2 +-
 src/backend/utils/sort/tuplesort.c             |  2 +-
 src/bin/pg_dump/pg_dump.h                      |  2 +-
 src/bin/pg_rewind/filemap.h                    |  2 +-
 src/bin/pgbench/pgbench.c                      |  2 +-
 src/include/access/brin_internal.h             |  2 +-
 src/include/access/tupdesc.h                   |  2 +-
 src/include/executor/execPartition.h           |  4 ++--
 src/include/executor/instrument.h              |  2 +-
 src/include/executor/instrument_node.h         | 18 +++++++++---------
 src/include/fe_utils/parallel_slot.h           |  2 +-
 src/include/jit/jit.h                          |  2 +-
 src/include/nodes/bitmapset.h                  |  2 +-
 src/include/regex/regguts.h                    |  4 ++--
 .../statistics/extended_stats_internal.h       |  2 +-
 src/include/statistics/statistics.h            |  8 ++++----
 src/include/tsearch/dicts/spell.h              |  2 +-
 src/include/utils/catcache.h                   |  2 +-
 src/include/utils/datetime.h                   |  2 +-
 src/pl/plpython/plpy_procedure.h               |  2 +-
 42 files changed, 57 insertions(+), 57 deletions(-)

diff --git a/contrib/hstore/hstore_io.c b/contrib/hstore/hstore_io.c
index 785aa308cde..8e047eed4fc 100644
--- a/contrib/hstore/hstore_io.c
+++ b/contrib/hstore/hstore_io.c
@@ -843,7 +843,7 @@ typedef struct RecordIOData
 	/* this field is used only if target type is domain over composite: */
 	void	   *domain_info;	/* opaque cache for domain checks */
 	int			ncolumns;
-	ColumnIOData columns[FLEXIBLE_ARRAY_MEMBER];
+	ColumnIOData columns[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(ncolumns);
 } RecordIOData;
 
 PG_FUNCTION_INFO_V1(hstore_from_record);
diff --git a/contrib/pageinspect/brinfuncs.c b/contrib/pageinspect/brinfuncs.c
index c64124b5b02..0b3df183887 100644
--- a/contrib/pageinspect/brinfuncs.c
+++ b/contrib/pageinspect/brinfuncs.c
@@ -34,7 +34,7 @@ PG_FUNCTION_INFO_V1(brin_revmap_data);
 typedef struct brin_column_state
 {
 	int			nstored;
-	FmgrInfo	outputFn[FLEXIBLE_ARRAY_MEMBER];
+	FmgrInfo	outputFn[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(nstored);
 } brin_column_state;
 
 
diff --git a/src/backend/access/brin/brin_minmax_multi.c b/src/backend/access/brin/brin_minmax_multi.c
index 1a6056a6b47..386f8fc760a 100644
--- a/src/backend/access/brin/brin_minmax_multi.c
+++ b/src/backend/access/brin/brin_minmax_multi.c
@@ -190,7 +190,7 @@ typedef struct Ranges
 	int			target_maxvalues;
 
 	/* values stored for this range - either raw values, or ranges */
-	Datum		values[FLEXIBLE_ARRAY_MEMBER];
+	Datum		values[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(maxvalues);
 } Ranges;
 
 /*
diff --git a/src/backend/access/nbtree/nbtutils.c b/src/backend/access/nbtree/nbtutils.c
index 014faa1622f..ef81ec50482 100644
--- a/src/backend/access/nbtree/nbtutils.c
+++ b/src/backend/access/nbtree/nbtutils.c
@@ -413,7 +413,7 @@ typedef struct BTVacInfo
 	BTCycleId	cycle_ctr;		/* cycle ID most recently assigned */
 	int			num_vacuums;	/* number of currently active VACUUMs */
 	int			max_vacuums;	/* allocated length of vacuums[] array */
-	BTOneVacInfo vacuums[FLEXIBLE_ARRAY_MEMBER];
+	BTOneVacInfo vacuums[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(max_vacuums);
 } BTVacInfo;
 
 static BTVacInfo *btvacinfo;
diff --git a/src/backend/access/transam/multixact.c b/src/backend/access/transam/multixact.c
index 3caedb54850..2ae1857a7a0 100644
--- a/src/backend/access/transam/multixact.c
+++ b/src/backend/access/transam/multixact.c
@@ -293,7 +293,7 @@ typedef struct mXactCacheEnt
 	MultiXactId multi;
 	int			nmembers;
 	dlist_node	node;
-	MultiXactMember members[FLEXIBLE_ARRAY_MEMBER];
+	MultiXactMember members[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(nmembers);
 } mXactCacheEnt;
 
 #define MAX_CACHE_ENTRIES	256
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index ebb010853cf..c22f95a8c4e 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -234,7 +234,7 @@ typedef struct SerializedTransactionState
 	FullTransactionId currentFullTransactionId;
 	CommandId	currentCommandId;
 	int			nParallelCurrentXids;
-	TransactionId parallelCurrentXids[FLEXIBLE_ARRAY_MEMBER];
+	TransactionId parallelCurrentXids[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(nParallelCurrentXids);
 } SerializedTransactionState;
 
 /* The size of SerializedTransactionState, not including the final array. */
diff --git a/src/backend/access/transam/xlogprefetcher.c b/src/backend/access/transam/xlogprefetcher.c
index dff57642ab4..011f0a98e74 100644
--- a/src/backend/access/transam/xlogprefetcher.c
+++ b/src/backend/access/transam/xlogprefetcher.c
@@ -117,7 +117,7 @@ typedef struct LsnReadQueue
 	{
 		bool		io;
 		XLogRecPtr	lsn;
-	}			queue[FLEXIBLE_ARRAY_MEMBER];
+	}			queue[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(size);
 } LsnReadQueue;
 
 /*
diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
index 2a46cc4de19..04171dfc5a9 100644
--- a/src/backend/catalog/index.c
+++ b/src/backend/catalog/index.c
@@ -96,7 +96,7 @@ typedef struct
 	Oid			currentlyReindexedHeap;
 	Oid			currentlyReindexedIndex;
 	int			numPendingReindexedIndexes;
-	Oid			pendingReindexedIndexes[FLEXIBLE_ARRAY_MEMBER];
+	Oid			pendingReindexedIndexes[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(numPendingReindexedIndexes);
 } SerializedReindexState;
 
 /* non-export function prototypes */
diff --git a/src/backend/commands/tablespace.c b/src/backend/commands/tablespace.c
index e01fb2db913..8c5f1a5a1e4 100644
--- a/src/backend/commands/tablespace.c
+++ b/src/backend/commands/tablespace.c
@@ -1216,7 +1216,7 @@ typedef struct
 {
 	/* Array of OIDs to be passed to SetTempTablespaces() */
 	int			numSpcs;
-	Oid			tblSpcs[FLEXIBLE_ARRAY_MEMBER];
+	Oid			tblSpcs[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(numSpcs);
 } temp_tablespaces_extra;
 
 /* check_hook: validate new temp_tablespaces */
diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.c
index 1d10fb1c13c..91d451d229e 100644
--- a/src/backend/commands/trigger.c
+++ b/src/backend/commands/trigger.c
@@ -3669,7 +3669,7 @@ typedef struct SetConstraintStateData
 	bool		all_isdeferred;
 	int			numstates;		/* number of trigstates[] entries in use */
 	int			numalloc;		/* allocated size of trigstates[] */
-	SetConstraintTriggerData trigstates[FLEXIBLE_ARRAY_MEMBER];
+	SetConstraintTriggerData trigstates[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(numalloc);
 } SetConstraintStateData;
 
 typedef SetConstraintStateData *SetConstraintState;
diff --git a/src/backend/executor/execParallel.c b/src/backend/executor/execParallel.c
index 6b85508a696..9d495673a73 100644
--- a/src/backend/executor/execParallel.c
+++ b/src/backend/executor/execParallel.c
@@ -103,7 +103,7 @@ struct SharedExecutorInstrumentation
 	int			instrument_offset;
 	int			num_workers;
 	int			num_plan_nodes;
-	int			plan_node_id[FLEXIBLE_ARRAY_MEMBER];
+	int			plan_node_id[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(num_plan_nodes);
 
 	/*
 	 * Array of num_plan_nodes * num_workers NodeInstrumentation objects
diff --git a/src/backend/nodes/bitmapset.c b/src/backend/nodes/bitmapset.c
index 4a12b047789..3668b8de462 100644
--- a/src/backend/nodes/bitmapset.c
+++ b/src/backend/nodes/bitmapset.c
@@ -1106,14 +1106,14 @@ bms_replace_members(Bitmapset *a, const Bitmapset *b)
 	if (a->nwords < b->nwords)
 		a = (Bitmapset *) repalloc(a, BITMAPSET_SIZE(b->nwords));
 
+	a->nwords = b->nwords;
+
 	i = 0;
 	do
 	{
 		a->words[i] = b->words[i];
 	} while (++i < b->nwords);
 
-	a->nwords = b->nwords;
-
 #ifdef REALLOCATE_BITMAPSETS
 
 	/*
diff --git a/src/backend/postmaster/checkpointer.c b/src/backend/postmaster/checkpointer.c
index 580c7944119..1d1431b7bb7 100644
--- a/src/backend/postmaster/checkpointer.c
+++ b/src/backend/postmaster/checkpointer.c
@@ -140,7 +140,7 @@ typedef struct
 								 * buffer */
 
 	/* The ring buffer of pending checkpointer requests */
-	CheckpointerRequest requests[FLEXIBLE_ARRAY_MEMBER];
+	CheckpointerRequest requests[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(max_requests);
 } CheckpointerShmemStruct;
 
 static CheckpointerShmemStruct *CheckpointerShmem;
diff --git a/src/backend/postmaster/launch_backend.c b/src/backend/postmaster/launch_backend.c
index 8f3cfea880c..8bb77e7880b 100644
--- a/src/backend/postmaster/launch_backend.c
+++ b/src/backend/postmaster/launch_backend.c
@@ -145,7 +145,7 @@ typedef struct
 	 * Extra startup data, content depends on the child process.
 	 */
 	size_t		startup_data_len;
-	char		startup_data[FLEXIBLE_ARRAY_MEMBER];
+	char		startup_data[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(startup_data_len);
 } BackendParameters;
 
 #define SizeOfBackendParameters(startup_data_len) (offsetof(BackendParameters, startup_data) + startup_data_len)
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index ede117d339e..8ce76d63718 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -171,7 +171,7 @@ typedef struct ReorderBufferIterTXNState
 	binaryheap *heap;
 	Size		nr_txns;
 	dlist_head	old_change;
-	ReorderBufferIterTXNEntry entries[FLEXIBLE_ARRAY_MEMBER];
+	ReorderBufferIterTXNEntry entries[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(nr_txns);
 } ReorderBufferIterTXNState;
 
 /* toast datastructures */
diff --git a/src/backend/storage/buffer/freelist.c b/src/backend/storage/buffer/freelist.c
index fdb5bad7910..6a3c002cfc0 100644
--- a/src/backend/storage/buffer/freelist.c
+++ b/src/backend/storage/buffer/freelist.c
@@ -90,7 +90,7 @@ typedef struct BufferAccessStrategyData
 	 * simplicity this is palloc'd together with the fixed fields of the
 	 * struct.
 	 */
-	Buffer		buffers[FLEXIBLE_ARRAY_MEMBER];
+	Buffer		buffers[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(nbuffers);
 }			BufferAccessStrategyData;
 
 
diff --git a/src/backend/storage/ipc/dsm.c b/src/backend/storage/ipc/dsm.c
index 8b69df4ff26..1c99e600765 100644
--- a/src/backend/storage/ipc/dsm.c
+++ b/src/backend/storage/ipc/dsm.c
@@ -93,7 +93,7 @@ typedef struct dsm_control_header
 	uint32		magic;
 	uint32		nitems;
 	uint32		maxitems;
-	dsm_control_item item[FLEXIBLE_ARRAY_MEMBER];
+	dsm_control_item item[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(maxitems);
 } dsm_control_header;
 
 static void dsm_cleanup_for_mmap(void);
diff --git a/src/backend/storage/ipc/pmsignal.c b/src/backend/storage/ipc/pmsignal.c
index bdad5fdd043..a1bb635fb42 100644
--- a/src/backend/storage/ipc/pmsignal.c
+++ b/src/backend/storage/ipc/pmsignal.c
@@ -78,7 +78,7 @@ struct PMSignalData
 	QuitSignalReason sigquit_reason;	/* why SIGQUIT was sent */
 	/* per-child-process flags */
 	int			num_child_flags;	/* # of entries in PMChildFlags[] */
-	sig_atomic_t PMChildFlags[FLEXIBLE_ARRAY_MEMBER];
+	sig_atomic_t PMChildFlags[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(num_child_flags);
 };
 
 /* PMSignalState pointer is valid in both postmaster and child processes */
diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c
index bc7b556e22e..64c85e9ddd1 100644
--- a/src/backend/utils/adt/jsonfuncs.c
+++ b/src/backend/utils/adt/jsonfuncs.c
@@ -233,7 +233,7 @@ struct RecordIOData
 	Oid			record_type;
 	int32		record_typmod;
 	int			ncolumns;
-	ColumnIOData columns[FLEXIBLE_ARRAY_MEMBER];
+	ColumnIOData columns[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(ncolumns);
 };
 
 /* per-query cache for populate_record_worker and populate_recordset_worker */
diff --git a/src/backend/utils/adt/rowtypes.c b/src/backend/utils/adt/rowtypes.c
index 0da0f10c2a0..ffa60aaaba0 100644
--- a/src/backend/utils/adt/rowtypes.c
+++ b/src/backend/utils/adt/rowtypes.c
@@ -45,7 +45,7 @@ typedef struct RecordIOData
 	Oid			record_type;
 	int32		record_typmod;
 	int			ncolumns;
-	ColumnIOData columns[FLEXIBLE_ARRAY_MEMBER];
+	ColumnIOData columns[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(ncolumns);
 } RecordIOData;
 
 /*
@@ -63,7 +63,7 @@ typedef struct RecordCompareData
 	int32		record1_typmod;
 	Oid			record2_type;
 	int32		record2_typmod;
-	ColumnCompareData columns[FLEXIBLE_ARRAY_MEMBER];
+	ColumnCompareData columns[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(ncolumns);
 } RecordCompareData;
 
 
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 0bd32a5619f..53de1d7b455 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -50,7 +50,7 @@ typedef struct StatEntry
 	struct StatEntry *left;
 	struct StatEntry *right;
 	uint32		lenlexeme;
-	char		lexeme[FLEXIBLE_ARRAY_MEMBER];
+	char		lexeme[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(lenlexeme);
 } StatEntry;
 
 #define STATENTRYHDRSZ	(offsetof(StatEntry, lexeme))
diff --git a/src/backend/utils/cache/typcache.c b/src/backend/utils/cache/typcache.c
index eca2d73231a..57c47be242a 100644
--- a/src/backend/utils/cache/typcache.c
+++ b/src/backend/utils/cache/typcache.c
@@ -155,7 +155,7 @@ typedef struct TypeCacheEnumData
 	Oid			bitmap_base;	/* OID corresponding to bit 0 of bitmapset */
 	Bitmapset  *sorted_values;	/* Set of OIDs known to be in order */
 	int			num_values;		/* total number of values in enum */
-	EnumItem	enum_values[FLEXIBLE_ARRAY_MEMBER];
+	EnumItem	enum_values[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(num_values);
 } TypeCacheEnumData;
 
 /*
diff --git a/src/backend/utils/sort/sharedtuplestore.c b/src/backend/utils/sort/sharedtuplestore.c
index 04189f708fa..5c6af636682 100644
--- a/src/backend/utils/sort/sharedtuplestore.c
+++ b/src/backend/utils/sort/sharedtuplestore.c
@@ -64,7 +64,7 @@ struct SharedTuplestore
 	char		name[NAMEDATALEN];	/* A name for this tuplestore. */
 
 	/* Followed by per-participant shared state. */
-	SharedTuplestoreParticipant participants[FLEXIBLE_ARRAY_MEMBER];
+	SharedTuplestoreParticipant participants[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(nparticipants);
 };
 
 /* Per-participant state that lives in backend-local memory. */
diff --git a/src/backend/utils/sort/tuplesort.c b/src/backend/utils/sort/tuplesort.c
index 37c40763ee0..868baaac05e 100644
--- a/src/backend/utils/sort/tuplesort.c
+++ b/src/backend/utils/sort/tuplesort.c
@@ -362,7 +362,7 @@ struct Sharedsort
 	 * Tapes array used by workers to report back information needed by the
 	 * leader to concatenate all worker tapes into one for merging
 	 */
-	TapeShare	tapes[FLEXIBLE_ARRAY_MEMBER];
+	TapeShare	tapes[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(nTapes);
 };
 
 /*
diff --git a/src/bin/pg_dump/pg_dump.h b/src/bin/pg_dump/pg_dump.h
index 2bbb5d5773b..1888c6f07ba 100644
--- a/src/bin/pg_dump/pg_dump.h
+++ b/src/bin/pg_dump/pg_dump.h
@@ -640,7 +640,7 @@ typedef struct _loInfo
 	DumpableAcl dacl;
 	const char *rolname;
 	int			numlos;
-	Oid			looids[FLEXIBLE_ARRAY_MEMBER];
+	Oid			looids[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(numlos);
 } LoInfo;
 
 /*
diff --git a/src/bin/pg_rewind/filemap.h b/src/bin/pg_rewind/filemap.h
index 4c6dd8740d7..c528aa74263 100644
--- a/src/bin/pg_rewind/filemap.h
+++ b/src/bin/pg_rewind/filemap.h
@@ -101,7 +101,7 @@ typedef struct filemap_t
 	uint64		fetch_size;		/* number of bytes that needs to be copied */
 
 	int			nentries;		/* size of 'entries' array */
-	file_entry_t *entries[FLEXIBLE_ARRAY_MEMBER];
+	file_entry_t *entries[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(nentries);
 } filemap_t;
 
 /* Functions for populating the filemap */
diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
index 181f0d892ef..d6318020a64 100644
--- a/src/bin/pgbench/pgbench.c
+++ b/src/bin/pgbench/pgbench.c
@@ -98,7 +98,7 @@ typedef struct socket_set
 {
 	int			maxfds;			/* allocated length of pollfds[] array */
 	int			curfds;			/* number currently in use */
-	struct pollfd pollfds[FLEXIBLE_ARRAY_MEMBER];
+	struct pollfd pollfds[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(maxfds);
 } socket_set;
 
 #endif							/* POLL_USING_PPOLL */
diff --git a/src/include/access/brin_internal.h b/src/include/access/brin_internal.h
index e17c7fee511..29fb4b870ae 100644
--- a/src/include/access/brin_internal.h
+++ b/src/include/access/brin_internal.h
@@ -34,7 +34,7 @@ typedef struct BrinOpcInfo
 	void	   *oi_opaque;
 
 	/* Type cache entries of the stored columns */
-	TypeCacheEntry *oi_typcache[FLEXIBLE_ARRAY_MEMBER];
+	TypeCacheEntry *oi_typcache[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(oi_nstored);
 } BrinOpcInfo;
 
 /* the size of a BrinOpcInfo for the given number of columns */
diff --git a/src/include/access/tupdesc.h b/src/include/access/tupdesc.h
index d26287271e9..8febd18cc3a 100644
--- a/src/include/access/tupdesc.h
+++ b/src/include/access/tupdesc.h
@@ -158,7 +158,7 @@ typedef struct TupleDescData
 										 * compact_attrs element. */
 	TupleConstr *constr;		/* constraints, or NULL if none */
 	/* compact_attrs[N] is the compact metadata of Attribute Number N+1 */
-	CompactAttribute compact_attrs[FLEXIBLE_ARRAY_MEMBER];
+	CompactAttribute compact_attrs[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(natts);
 }			TupleDescData;
 typedef struct TupleDescData *TupleDesc;
 
diff --git a/src/include/executor/execPartition.h b/src/include/executor/execPartition.h
index 82063ec2a16..9c029c9340c 100644
--- a/src/include/executor/execPartition.h
+++ b/src/include/executor/execPartition.h
@@ -84,7 +84,7 @@ typedef struct PartitionedRelPruningData
 typedef struct PartitionPruningData
 {
 	int			num_partrelprunedata;	/* number of array entries */
-	PartitionedRelPruningData partrelprunedata[FLEXIBLE_ARRAY_MEMBER];
+	PartitionedRelPruningData partrelprunedata[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(num_partrelprunedata);
 } PartitionPruningData;
 
 /*
@@ -127,7 +127,7 @@ typedef struct PartitionPruneState
 	bool		do_initial_prune;
 	bool		do_exec_prune;
 	int			num_partprunedata;
-	PartitionPruningData *partprunedata[FLEXIBLE_ARRAY_MEMBER];
+	PartitionPruningData *partprunedata[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(num_partprunedata);
 } PartitionPruneState;
 
 extern void ExecDoInitialPruning(EState *estate);
diff --git a/src/include/executor/instrument.h b/src/include/executor/instrument.h
index f093a52aae0..d955095c882 100644
--- a/src/include/executor/instrument.h
+++ b/src/include/executor/instrument.h
@@ -115,7 +115,7 @@ typedef struct NodeInstrumentation
 typedef struct WorkerNodeInstrumentation
 {
 	int			num_workers;	/* # of structures that follow */
-	NodeInstrumentation instrument[FLEXIBLE_ARRAY_MEMBER];
+	NodeInstrumentation instrument[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(num_workers);
 } WorkerNodeInstrumentation;
 
 typedef struct TriggerInstrumentation
diff --git a/src/include/executor/instrument_node.h b/src/include/executor/instrument_node.h
index 41a7d33f19c..ec690197b04 100644
--- a/src/include/executor/instrument_node.h
+++ b/src/include/executor/instrument_node.h
@@ -44,7 +44,7 @@ typedef struct AggregateInstrumentation
 typedef struct SharedAggInfo
 {
 	int			num_workers;
-	AggregateInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER];
+	AggregateInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(num_workers);
 } SharedAggInfo;
 
 
@@ -116,7 +116,7 @@ typedef struct IndexScanInstrumentation
 typedef struct SharedIndexScanInstrumentation
 {
 	int			num_workers;
-	IndexScanInstrumentation winstrument[FLEXIBLE_ARRAY_MEMBER];
+	IndexScanInstrumentation winstrument[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(num_workers);
 } SharedIndexScanInstrumentation;
 
 
@@ -140,7 +140,7 @@ typedef struct BitmapHeapScanInstrumentation
 typedef struct SharedBitmapHeapInstrumentation
 {
 	int			num_workers;
-	BitmapHeapScanInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER];
+	BitmapHeapScanInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(num_workers);
 } SharedBitmapHeapInstrumentation;
 
 
@@ -169,7 +169,7 @@ typedef struct MemoizeInstrumentation
 typedef struct SharedMemoizeInfo
 {
 	int			num_workers;
-	MemoizeInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER];
+	MemoizeInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(num_workers);
 } SharedMemoizeInfo;
 
 
@@ -215,7 +215,7 @@ typedef struct TuplesortInstrumentation
 typedef struct SharedSortInfo
 {
 	int			num_workers;
-	TuplesortInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER];
+	TuplesortInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(num_workers);
 } SharedSortInfo;
 
 
@@ -238,7 +238,7 @@ typedef struct HashInstrumentation
 typedef struct SharedHashInfo
 {
 	int			num_workers;
-	HashInstrumentation hinstrument[FLEXIBLE_ARRAY_MEMBER];
+	HashInstrumentation hinstrument[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(num_workers);
 } SharedHashInfo;
 
 
@@ -266,7 +266,7 @@ typedef struct IncrementalSortInfo
 typedef struct SharedIncrementalSortInfo
 {
 	int			num_workers;
-	IncrementalSortInfo sinfo[FLEXIBLE_ARRAY_MEMBER];
+	IncrementalSortInfo sinfo[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(num_workers);
 } SharedIncrementalSortInfo;
 
 
@@ -285,7 +285,7 @@ typedef struct SeqScanInstrumentation
 typedef struct SharedSeqScanInstrumentation
 {
 	int			num_workers;
-	SeqScanInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER];
+	SeqScanInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(num_workers);
 } SharedSeqScanInstrumentation;
 
 
@@ -303,7 +303,7 @@ typedef struct TidRangeScanInstrumentation
 typedef struct SharedTidRangeScanInstrumentation
 {
 	int			num_workers;
-	TidRangeScanInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER];
+	TidRangeScanInstrumentation sinstrument[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(num_workers);
 } SharedTidRangeScanInstrumentation;
 
 #endif							/* INSTRUMENT_NODE_H */
diff --git a/src/include/fe_utils/parallel_slot.h b/src/include/fe_utils/parallel_slot.h
index a6ebe273ce0..24f1fe3942f 100644
--- a/src/include/fe_utils/parallel_slot.h
+++ b/src/include/fe_utils/parallel_slot.h
@@ -40,7 +40,7 @@ typedef struct ParallelSlotArray
 	const char *progname;
 	bool		echo;
 	const char *initcmd;
-	ParallelSlot slots[FLEXIBLE_ARRAY_MEMBER];
+	ParallelSlot slots[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(numslots);
 } ParallelSlotArray;
 
 static inline void
diff --git a/src/include/jit/jit.h b/src/include/jit/jit.h
index e2baa4c2ed0..8ffa60a2001 100644
--- a/src/include/jit/jit.h
+++ b/src/include/jit/jit.h
@@ -51,7 +51,7 @@ typedef struct JitInstrumentation
 typedef struct SharedJitInstrumentation
 {
 	int			num_workers;
-	JitInstrumentation jit_instr[FLEXIBLE_ARRAY_MEMBER];
+	JitInstrumentation jit_instr[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(num_workers);
 } SharedJitInstrumentation;
 
 typedef struct JitContext
diff --git a/src/include/nodes/bitmapset.h b/src/include/nodes/bitmapset.h
index 2f0b1f04bb7..350b9bb189e 100644
--- a/src/include/nodes/bitmapset.h
+++ b/src/include/nodes/bitmapset.h
@@ -52,7 +52,7 @@ typedef struct Bitmapset
 
 	NodeTag		type;
 	int			nwords;			/* number of words in array */
-	bitmapword	words[FLEXIBLE_ARRAY_MEMBER];	/* really [nwords] */
+	bitmapword	words[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(nwords);
 } Bitmapset;
 
 
diff --git a/src/include/regex/regguts.h b/src/include/regex/regguts.h
index 6fb9551721d..fce0f8981b5 100644
--- a/src/include/regex/regguts.h
+++ b/src/include/regex/regguts.h
@@ -320,7 +320,7 @@ struct arcbatch
 {								/* for bulk allocation of arcs */
 	struct arcbatch *next;		/* chain link */
 	size_t		narcs;			/* number of arcs allocated in this arcbatch */
-	struct arc	a[FLEXIBLE_ARRAY_MEMBER];
+	struct arc	a[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(narcs);
 };
 #define  ARCBATCHSIZE(n)  ((n) * sizeof(struct arc) + offsetof(struct arcbatch, a))
 /* first batch will have FIRSTABSIZE arcs; then double it until MAXABSIZE */
@@ -346,7 +346,7 @@ struct statebatch
 {								/* for bulk allocation of states */
 	struct statebatch *next;	/* chain link */
 	size_t		nstates;		/* number of states allocated in this batch */
-	struct state s[FLEXIBLE_ARRAY_MEMBER];
+	struct state s[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(nstates);
 };
 #define  STATEBATCHSIZE(n)  ((n) * sizeof(struct state) + offsetof(struct statebatch, s))
 /* first batch will have FIRSTSBSIZE states; then double it until MAXSBSIZE */
diff --git a/src/include/statistics/extended_stats_internal.h b/src/include/statistics/extended_stats_internal.h
index c775442f2ee..db64e45f036 100644
--- a/src/include/statistics/extended_stats_internal.h
+++ b/src/include/statistics/extended_stats_internal.h
@@ -45,7 +45,7 @@ typedef struct MultiSortSupportData
 {
 	int			ndims;			/* number of dimensions */
 	/* sort support data for each dimension: */
-	SortSupportData ssup[FLEXIBLE_ARRAY_MEMBER];
+	SortSupportData ssup[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(ndims);
 } MultiSortSupportData;
 
 typedef MultiSortSupportData *MultiSortSupport;
diff --git a/src/include/statistics/statistics.h b/src/include/statistics/statistics.h
index 0b163103a72..e20f3160cb9 100644
--- a/src/include/statistics/statistics.h
+++ b/src/include/statistics/statistics.h
@@ -36,7 +36,7 @@ typedef struct MVNDistinct
 	uint32		magic;			/* magic constant marker */
 	uint32		type;			/* type of ndistinct (BASIC) */
 	uint32		nitems;			/* number of items in the statistic */
-	MVNDistinctItem items[FLEXIBLE_ARRAY_MEMBER];
+	MVNDistinctItem items[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(nitems);
 } MVNDistinct;
 
 /* Multivariate functional dependencies */
@@ -51,7 +51,7 @@ typedef struct MVDependency
 {
 	double		degree;			/* degree of validity (0-1) */
 	AttrNumber	nattributes;	/* number of attributes */
-	AttrNumber	attributes[FLEXIBLE_ARRAY_MEMBER];	/* attribute numbers */
+	AttrNumber	attributes[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(nattributes); /* attribute numbers */
 } MVDependency;
 
 typedef struct MVDependencies
@@ -59,7 +59,7 @@ typedef struct MVDependencies
 	uint32		magic;			/* magic constant marker */
 	uint32		type;			/* type of MV Dependencies (BASIC) */
 	uint32		ndeps;			/* number of dependencies */
-	MVDependency *deps[FLEXIBLE_ARRAY_MEMBER];	/* dependencies */
+	MVDependency *deps[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(ndeps);	/* dependencies */
 } MVDependencies;
 
 /* used to flag stats serialized to bytea */
@@ -91,7 +91,7 @@ typedef struct MCVList
 	uint32		nitems;			/* number of MCV items in the array */
 	AttrNumber	ndimensions;	/* number of dimensions */
 	Oid			types[STATS_MAX_DIMENSIONS];	/* OIDs of data types */
-	MCVItem		items[FLEXIBLE_ARRAY_MEMBER];	/* array of MCV items */
+	MCVItem		items[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(nitems);	/* array of MCV items */
 } MCVList;
 
 extern MVNDistinct *statext_ndistinct_load(Oid mvoid, bool inh);
diff --git a/src/include/tsearch/dicts/spell.h b/src/include/tsearch/dicts/spell.h
index 7ca7e6fe69f..315c74e3d57 100644
--- a/src/include/tsearch/dicts/spell.h
+++ b/src/include/tsearch/dicts/spell.h
@@ -50,7 +50,7 @@ typedef struct
 typedef struct SPNode
 {
 	uint32		length;
-	SPNodeData	data[FLEXIBLE_ARRAY_MEMBER];
+	SPNodeData	data[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(length);
 } SPNode;
 
 #define SPNHDRSZ	(offsetof(SPNode,data))
diff --git a/src/include/utils/catcache.h b/src/include/utils/catcache.h
index a28a1e483eb..b9bbaa9cd67 100644
--- a/src/include/utils/catcache.h
+++ b/src/include/utils/catcache.h
@@ -179,7 +179,7 @@ typedef struct catclist
 	short		nkeys;			/* number of lookup keys specified */
 	int			n_members;		/* number of member tuples */
 	CatCache   *my_cache;		/* link to owning catcache */
-	CatCTup    *members[FLEXIBLE_ARRAY_MEMBER]; /* members */
+	CatCTup    *members[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(n_members);	/* members */
 } CatCList;
 
 
diff --git a/src/include/utils/datetime.h b/src/include/utils/datetime.h
index 87c50eebf12..0cda0352bcc 100644
--- a/src/include/utils/datetime.h
+++ b/src/include/utils/datetime.h
@@ -216,7 +216,7 @@ typedef struct TimeZoneAbbrevTable
 {
 	Size		tblsize;		/* size in bytes of TimeZoneAbbrevTable */
 	int			numabbrevs;		/* number of entries in abbrevs[] array */
-	datetkn		abbrevs[FLEXIBLE_ARRAY_MEMBER];
+	datetkn		abbrevs[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(numabbrevs);
 	/* DynamicZoneAbbrev(s) may follow the abbrevs[] array */
 } TimeZoneAbbrevTable;
 
diff --git a/src/pl/plpython/plpy_procedure.h b/src/pl/plpython/plpy_procedure.h
index 4103fd22945..0532c4ba72a 100644
--- a/src/pl/plpython/plpy_procedure.h
+++ b/src/pl/plpython/plpy_procedure.h
@@ -26,7 +26,7 @@ typedef struct PLySavedArgs
 	PyObject   *args;			/* "args" element of globals dict */
 	PyObject   *td;				/* "TD" element of globals dict, if trigger */
 	int			nargs;			/* length of namedargs array */
-	PyObject   *namedargs[FLEXIBLE_ARRAY_MEMBER];	/* named args */
+	PyObject   *namedargs[FLEXIBLE_ARRAY_MEMBER] pg_attribute_counted_by(nargs);	/* named args */
 } PLySavedArgs;
 
 /* saved state for a set-returning function */
-- 
Tristan Partin
https://tristan.partin.io

