From 0fe1eff8efc21979eb8e2d65b2fe1bc476c28c90 Mon Sep 17 00:00:00 2001
From: Zsolt Parragi <zsolt.parragi@percona.com>
Date: Mon, 31 Aug 2026 12:00:35 +0000
Subject: [PATCH v3 1/4] pgbench: don't disable --unlogged-tables when
 partitioning

With --partitions, --unlogged-tables was silently ignored for all of
pgbench's tables; only the pgbench_accounts partitions were created
unlogged.  The documentation promises unlogged tables regardless.

Create the other tables unlogged as requested.  pgbench_accounts
itself has to stay permanent, because a partitioned table cannot be
unlogged, and pgbench_branches is kept permanent too, so that
--foreign-keys can still reference it from the permanent
pgbench_accounts: constraints on permanent tables may reference only
permanent tables.

This also stops pgbench_history from being a permanent table that
references the unlogged pgbench_accounts partitions, a combination
that leaves dangling rows in pgbench_history after crash recovery and
that the server is about to reject.
---
 doc/src/sgml/ref/pgbench.sgml |  7 +++++++
 src/bin/pgbench/pgbench.c     | 15 ++++++++++++++-
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/doc/src/sgml/ref/pgbench.sgml b/doc/src/sgml/ref/pgbench.sgml
index 9688527c955..356caf1eedf 100644
--- a/doc/src/sgml/ref/pgbench.sgml
+++ b/doc/src/sgml/ref/pgbench.sgml
@@ -400,6 +400,13 @@ pgbench <optional> <replaceable>options</replaceable> </optional> <replaceable>d
       <listitem>
        <para>
         Create all tables as unlogged tables, rather than permanent tables.
+        When <option>--partitions</option> is used,
+        <structname>pgbench_accounts</structname> is nevertheless created as a
+        permanent table, because a partitioned table cannot be unlogged; its
+        partitions are created unlogged.  <structname>pgbench_branches</structname>
+        is then also kept permanent, so that
+        <option>--foreign-keys</option> can reference it from the permanent
+        <structname>pgbench_accounts</structname>.
        </para>
       </listitem>
      </varlistentry>
diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
index 181f0d892ef..23b75ae9ebd 100644
--- a/src/bin/pgbench/pgbench.c
+++ b/src/bin/pgbench/pgbench.c
@@ -4961,9 +4961,22 @@ initCreateTables(PGconn *con)
 	{
 		const struct ddlinfo *ddl = &DDLs[i];
 
+		bool		unlogged = unlogged_tables;
+
+		/*
+		 * When the accounts table is partitioned, it cannot be unlogged
+		 * itself; its partitions are created unlogged instead.  In that case
+		 * pgbench_branches must stay permanent too, so that the permanent
+		 * pgbench_accounts can reference it when --foreign-keys is used.
+		 */
+		if (partition_method != PART_NONE &&
+			(strcmp(ddl->table, "pgbench_accounts") == 0 ||
+			 strcmp(ddl->table, "pgbench_branches") == 0))
+			unlogged = false;
+
 		/* Construct new create table statement. */
 		printfPQExpBuffer(&query, "create%s table %s(%s)",
-						  (unlogged_tables && partition_method == PART_NONE) ? " unlogged" : "",
+						  unlogged ? " unlogged" : "",
 						  ddl->table,
 						  (scale >= SCALE_32BIT_THRESHOLD) ? ddl->bigcols : ddl->smcols);
 
-- 
2.56.0

