# Copyright (c) 2026, PostgreSQL Global Development Group use strict; use warnings FATAL => 'all'; use PostgreSQL::Test::Cluster; use PostgreSQL::Test::Utils; use Test::More; my $node = PostgreSQL::Test::Cluster->new('main'); $node->init; $node->append_conf('postgresql.conf', 'autovacuum = off'); $node->start; sub persistent_insert { my ($value, $guard_settings) = @_; $node->safe_psql( 'postgres', qq{ LOAD 'online_analyze'; SET online_analyze.table_type = 'persistent'; SELECT set_config('online_analyze.verbose', 'off', false); $guard_settings INSERT INTO persistent_guard VALUES ($value); }); } sub persistent_last_analyze { return $node->safe_psql( 'postgres', q{ WITH cleared AS MATERIALIZED (SELECT pg_stat_clear_snapshot()) SELECT last_analyze::text FROM cleared, pg_stat_all_tables WHERE relid = 'persistent_guard'::regclass; }); } my $prohibitive_guards = q{ SET online_analyze.scale_factor = 1.0; SET online_analyze.threshold = 2147483647; SET online_analyze.lower_limit = 2147483647; SET online_analyze.min_interval = 86400000; }; $node->safe_psql( 'postgres', q{ CREATE TABLE persistent_guard (i integer); INSERT INTO persistent_guard SELECT generate_series(1, 10); ANALYZE persistent_guard; }); my $persistent_before = persistent_last_analyze(); persistent_insert(11, $prohibitive_guards); my $persistent_after_first = persistent_last_analyze(); is($persistent_after_first, $persistent_before, 'persistent guards prevent ANALYZE after first modification'); persistent_insert(12, $prohibitive_guards); my $persistent_after_second = persistent_last_analyze(); is($persistent_after_second, $persistent_before, 'persistent guards prevent ANALYZE after second modification'); $node->safe_psql('postgres', 'SELECT pg_sleep(0.01)'); persistent_insert( 13, q{ SET online_analyze.scale_factor = 0; SET online_analyze.threshold = 0; SET online_analyze.lower_limit = 0; SET online_analyze.min_interval = 0; }); my $persistent_after_positive = persistent_last_analyze(); isnt($persistent_after_positive, $persistent_before, 'relaxed guards allow persistent online ANALYZE'); my $temporary_results = $node->safe_psql( 'postgres', qq{ CREATE TEMP TABLE temporary_guard (i integer); INSERT INTO temporary_guard VALUES (1); ANALYZE temporary_guard; DO LANGUAGE plpgsql 'BEGIN PERFORM pg_sleep(0.01); END'; LOAD 'online_analyze'; SET online_analyze.table_type = 'temporary'; SET online_analyze.local_tracking = on; DO LANGUAGE plpgsql 'BEGIN PERFORM set_config(''online_analyze.verbose'', ''off'', false); END'; $prohibitive_guards SELECT last_analyze::text FROM pg_stat_all_tables WHERE relid = 'temporary_guard'::regclass; INSERT INTO temporary_guard VALUES (2); WITH cleared AS MATERIALIZED (SELECT pg_stat_clear_snapshot()) SELECT last_analyze::text FROM cleared, pg_stat_all_tables WHERE relid = 'temporary_guard'::regclass; INSERT INTO temporary_guard VALUES (3); WITH cleared AS MATERIALIZED (SELECT pg_stat_clear_snapshot()) SELECT last_analyze::text FROM cleared, pg_stat_all_tables WHERE relid = 'temporary_guard'::regclass; }); my ($temporary_before, $temporary_after_first, $temporary_after_second) = split /\n/, $temporary_results; isnt($temporary_after_first, $temporary_before, 'first temporary modification retains new-table ANALYZE'); is($temporary_after_second, $temporary_after_first, 'second temporary modification obeys prohibitive guards'); $node->stop; done_testing();