From 25930bccab22105ce873b4dd84a222da1e0e31cb Mon Sep 17 00:00:00 2001
From: Andrew Dunstan <andrew@dunslane.net>
Date: Tue, 1 Dec 2026 13:24:25 -0500
Subject: [PATCH] sepgsql: keep exercising the FK validation query in the alter
 test

Commit 00f27019882 taught validateForeignKeyConstraint() to skip the
RI_Initial_Check() SPI query when the referencing table is empty.  The
sepgsql alter test adds a foreign key to an empty table, so the
permission checks on that query were no longer performed or logged, and
the test failed on buildfarm members running sepgsql.

Rather than just removing those lines from the expected output, give the
referencing table a row before adding the constraint so the query still
runs and its permission checks stay covered.  The row is removed again
afterwards, with auditing silenced, so the rest of the test is
unaffected.
---
 contrib/sepgsql/expected/alter.out | 8 ++++++++
 contrib/sepgsql/sql/alter.sql      | 9 +++++++++
 2 files changed, 17 insertions(+)

diff --git a/contrib/sepgsql/expected/alter.out b/contrib/sepgsql/expected/alter.out
index 1462cfa3cbc..8c373a832b2 100644
--- a/contrib/sepgsql/expected/alter.out
+++ b/contrib/sepgsql/expected/alter.out
@@ -26,6 +26,10 @@ SET search_path = regtest_schema_1, regtest_schema_2, public;
 CREATE TABLE regtest_table_1 (a int, b text);
 CREATE TABLE regtest_table_2 (c text) inherits (regtest_table_1);
 CREATE TABLE regtest_table_3 (x int primary key, y text);
+-- The FK added below is only checked against the referenced table when the
+-- referencing table is non-empty, so give it a row to check.
+INSERT INTO regtest_table_1 VALUES (1, 'abc');
+INSERT INTO regtest_table_3 VALUES (1, 'abc');
 ---
 -- partitioned table parent
 CREATE TABLE regtest_ptable_1 (o int, p text) PARTITION BY RANGE (o);
@@ -199,6 +203,10 @@ LOG:  SELinux: allowed { select } scontext=unconfined_u:unconfined_r:sepgsql_reg
 LOG:  SELinux: allowed { select } scontext=unconfined_u:unconfined_r:sepgsql_regtest_superuser_t:s0 tcontext=unconfined_u:object_r:sepgsql_table_t:s0 tclass=db_table name="regtest_schema.regtest_table_3" permissive=0
 LOG:  SELinux: allowed { select } scontext=unconfined_u:unconfined_r:sepgsql_regtest_superuser_t:s0 tcontext=unconfined_u:object_r:sepgsql_table_t:s0 tclass=db_column name="column x of table regtest_table_3" permissive=0
 LOG:  SELinux: allowed { execute } scontext=unconfined_u:unconfined_r:sepgsql_regtest_superuser_t:s0 tcontext=system_u:object_r:sepgsql_proc_exec_t:s0 tclass=db_procedure name="pg_catalog.int4eq(integer,integer)" permissive=0
+-- remove the row again (quietly), so later commands see an empty table
+SET sepgsql.debug_audit = false;
+DELETE FROM regtest_table;
+SET sepgsql.debug_audit = true;
 ALTER TABLE regtest_table ADD CONSTRAINT test_ck CHECK (b like '%abc%') NOT VALID;      -- not supported
 ALTER TABLE regtest_table VALIDATE CONSTRAINT test_ck;  -- not supported
 LOG:  SELinux: allowed { execute } scontext=unconfined_u:unconfined_r:sepgsql_regtest_superuser_t:s0 tcontext=system_u:object_r:sepgsql_proc_exec_t:s0 tclass=db_procedure name="pg_catalog.textlike(pg_catalog.text,pg_catalog.text)" permissive=0
diff --git a/contrib/sepgsql/sql/alter.sql b/contrib/sepgsql/sql/alter.sql
index fd711f3f7ab..e53ad51bb39 100644
--- a/contrib/sepgsql/sql/alter.sql
+++ b/contrib/sepgsql/sql/alter.sql
@@ -32,6 +32,11 @@ CREATE TABLE regtest_table_2 (c text) inherits (regtest_table_1);
 
 CREATE TABLE regtest_table_3 (x int primary key, y text);
 
+-- The FK added below is only checked against the referenced table when the
+-- referencing table is non-empty, so give it a row to check.
+INSERT INTO regtest_table_1 VALUES (1, 'abc');
+INSERT INTO regtest_table_3 VALUES (1, 'abc');
+
 ---
 -- partitioned table parent
 CREATE TABLE regtest_ptable_1 (o int, p text) PARTITION BY RANGE (o);
@@ -120,6 +125,10 @@ ALTER TABLE regtest_table ALTER b SET STATISTICS -1;
 ALTER TABLE regtest_table ALTER b SET (n_distinct = 999);
 ALTER TABLE regtest_table ALTER b SET STORAGE PLAIN;
 ALTER TABLE regtest_table ADD CONSTRAINT test_fk FOREIGN KEY (a) REFERENCES regtest_table_3(x); -- not supported
+-- remove the row again (quietly), so later commands see an empty table
+SET sepgsql.debug_audit = false;
+DELETE FROM regtest_table;
+SET sepgsql.debug_audit = true;
 ALTER TABLE regtest_table ADD CONSTRAINT test_ck CHECK (b like '%abc%') NOT VALID;      -- not supported
 ALTER TABLE regtest_table VALIDATE CONSTRAINT test_ck;  -- not supported
 ALTER TABLE regtest_table DROP CONSTRAINT test_ck;      -- not supported
-- 
2.55.0

