From a3876cd2cf54f0965c5c82df6acb87f1ea1c5d88 Mon Sep 17 00:00:00 2001 From: roman khapov Date: Mon, 2 Nov 2026 11:30:15 +0500 Subject: [PATCH v2] pgbench: honor --unlogged-tables for non-partitioned tables with --partitions The --unlogged-tables tweak from e2bab2d7920 was too broad: with --partitions=N it silently downgraded not only pgbench_accounts' partitioned parent, which cannot be unlogged anymore, but also the plain tables pgbench_branches, pgbench_tellers and pgbench_history, although the server happily creates those unlogged. Keep PERMANENT only the partitioned parent, and declare the accounts -> branches foreign key on each partition when unlogged tables are combined with partitions and --foreign-keys: the permanent parent may not reference an unlogged table, while each unlogged partition may. In default (non-unlogged) runs the constraint is still declared on the parent, as before. Reported-by: Kirill Reshke Suggested-by: Fujii Masao --- src/bin/pgbench/pgbench.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c index 06c43fd6942..6c431fadf31 100644 --- a/src/bin/pgbench/pgbench.c +++ b/src/bin/pgbench/pgbench.c @@ -4963,7 +4963,9 @@ initCreateTables(PGconn *con) /* Construct new create table statement. */ printfPQExpBuffer(&query, "create%s table %s(%s)", - (unlogged_tables && partition_method == PART_NONE) ? " unlogged" : "", + (unlogged_tables && + (partition_method == PART_NONE || + strcmp(ddl->table, "pgbench_accounts") != 0)) ? " unlogged" : "", ddl->table, (scale >= SCALE_32BIT_THRESHOLD) ? ddl->bigcols : ddl->smcols); @@ -5290,9 +5292,10 @@ initCreatePKeys(PGconn *con) static void initCreateFKeys(PGconn *con) { + PQExpBufferData query; + static const char *const DDLKEYs[] = { "alter table pgbench_tellers add constraint pgbench_tellers_bid_fkey foreign key (bid) references pgbench_branches", - "alter table pgbench_accounts add constraint pgbench_accounts_bid_fkey foreign key (bid) references pgbench_branches", "alter table pgbench_history add constraint pgbench_history_bid_fkey foreign key (bid) references pgbench_branches", "alter table pgbench_history add constraint pgbench_history_tid_fkey foreign key (tid) references pgbench_tellers", "alter table pgbench_history add constraint pgbench_history_aid_fkey foreign key (aid) references pgbench_accounts" @@ -5303,6 +5306,35 @@ initCreateFKeys(PGconn *con) { executeStatement(con, DDLKEYs[i]); } + + /* + * The accounts -> branches foreign key cannot be declared on the + * partitioned parent when unlogged tables are used: the parent stays + * PERMANENT while pgbench_branches is unlogged, and constraints on + * permanent tables may reference only permanent tables. Declare it on + * each partition instead, which is allowed as the partitions themselves + * are unlogged. + */ + if (unlogged_tables && partition_method != PART_NONE) + { + initPQExpBuffer(&query); + + for (int p = 1; p <= partitions; p++) + { + printfPQExpBuffer(&query, + "alter table pgbench_accounts_%d add constraint " + "pgbench_accounts_bid_fkey " + "foreign key (bid) references pgbench_branches", p); + executeStatement(con, query.data); + } + + termPQExpBuffer(&query); + } + else + executeStatement(con, + "alter table pgbench_accounts add constraint " + "pgbench_accounts_bid_fkey " + "foreign key (bid) references pgbench_branches"); } /* -- 2.50.1 (Apple Git-155)