提交 1b52d013 编写于 作者: H Heikki Linnakangas

Add a test case for autovacuum on template0.

Commit 4e655714 enabled autovacuum for template0, but didn't include any
tests to check that autovacuum really runs on template0. This commit adds
that.

To make this run faster, make debug_burn_xids burn 4096 XIDs instead of
1024.
Co-authored-by: NXin Zhang <xzhang@pivotal.io>
上级 618c7780
......@@ -190,7 +190,7 @@ GetNewTransactionId(bool isSubXact)
/*
* To aid testing, you can set the debug_burn_xids GUC, to consume XIDs
* faster. If set, we bump the XID counter to the next value divisible by
* 1024, minus one. The idea is to skip over "boring" XID ranges, but
* 4096, minus one. The idea is to skip over "boring" XID ranges, but
* still step through XID wraparound, CLOG page boundaries etc. one XID
* at a time.
*/
......@@ -199,12 +199,18 @@ GetNewTransactionId(bool isSubXact)
TransactionId xx;
uint32 r;
/*
* Based on the minimum of ENTRIES_PER_PAGE (DistributedLog),
* SUBTRANS_XACTS_PER_PAGE, CLOG_XACTS_PER_PAGE.
*/
const uint32 page_extend_limit = 4 * 1024;
xx = ShmemVariableCache->nextXid;
r = xx % 1024;
if (r > 1 && r < 1023)
r = xx % page_extend_limit;
if (r > 1 && r < (page_extend_limit - 1))
{
xx += 1024 - r - 1;
xx += page_extend_limit - r - 1;
ShmemVariableCache->nextXid = xx;
}
}
......
......@@ -208,4 +208,6 @@ test: psql_gp_commands pg_resetxlog
# Check for shmem leak for instrumentation slots
test: instr_in_shmem_verify
test: autovacuum-template0
# end of tests
create or replace function test_consume_xids(int4) returns void
as '@abs_srcdir@/regress.so', 'test_consume_xids'
language C;
set debug_burn_xids=on;
-- Autovacuum should take care of anti-XID wraparounds of 'template0'. Because
-- of that, the age of template0 should not go much above
-- autovacuum_freeze_max_age (we assume the default of 200 million here).
select age(datfrozenxid) < 200 * 1000000 from pg_database where datname='template0';
select test_consume_xids(100 * 1000000);
select test_consume_xids(100 * 1000000);
select test_consume_xids(10 * 1000000);
-- Wait until autovacuum has processed template0. (But give up after 2 minutes)
do $$
begin
for i in 1..120 loop
if (select age(datfrozenxid) < 200 * 1000000 from pg_database where datname='template0') then
raise notice 'template0 is young again';
return;
end if;
perform pg_sleep(1);
end loop;
raise notice 'FAIL: template0 is not being frozen!';
end;
$$;
-- But autovacuum should not touch other databases. Hence, our database
-- should be well above the 200 million mark.
select age(datfrozenxid) > 200 * 1000000 from pg_database where datname=current_database();
create or replace function test_consume_xids(int4) returns void
as '@abs_srcdir@/regress.so', 'test_consume_xids'
language C;
set debug_burn_xids=on;
-- Autovacuum should take care of anti-XID wraparounds of 'template0'. Because
-- of that, the age of template0 should not go much above
-- autovacuum_freeze_max_age (we assume the default of 200 million here).
select age(datfrozenxid) < 200 * 1000000 from pg_database where datname='template0';
?column?
----------
t
(1 row)
select test_consume_xids(100 * 1000000);
test_consume_xids
-------------------
(1 row)
select test_consume_xids(100 * 1000000);
test_consume_xids
-------------------
(1 row)
select test_consume_xids(10 * 1000000);
test_consume_xids
-------------------
(1 row)
-- Wait until autovacuum has processed template0. (But give up after 2 minutes)
do $$
begin
for i in 1..120 loop
if (select age(datfrozenxid) < 200 * 1000000 from pg_database where datname='template0') then
raise notice 'template0 is young again';
return;
end if;
perform pg_sleep(1);
end loop;
raise notice 'FAIL: template0 is not being frozen!';
end;
$$;
NOTICE: template0 is young again
-- But autovacuum should not touch other databases. Hence, our database
-- should be well above the 200 million mark.
select age(datfrozenxid) > 200 * 1000000 from pg_database where datname=current_database();
?column?
----------
t
(1 row)
......@@ -83,9 +83,12 @@ extern Datum assign_new_record(PG_FUNCTION_ARGS);
extern Datum udf_setenv(PG_FUNCTION_ARGS);
extern Datum udf_unsetenv(PG_FUNCTION_ARGS);
/* Aut Constraints */
/* Auth Constraints */
extern Datum check_auth_time_constraints(PG_FUNCTION_ARGS);
/* XID wraparound */
extern Datum test_consume_xids(PG_FUNCTION_ARGS);
/* Triggers */
typedef struct
......@@ -1912,3 +1915,36 @@ trigger_udf_return_new_oid(PG_FUNCTION_ARGS)
return PointerGetDatum(ret_tuple);
}
/*
* test_consume_xids(int4), for rapidly consuming XIDs, to test wraparound.
*
* Used by the 'autovacuum-template0' test.
*/
PG_FUNCTION_INFO_V1(test_consume_xids);
Datum
test_consume_xids(PG_FUNCTION_ARGS)
{
int32 nxids = PG_GETARG_INT32(0);
TransactionId topxid;
TransactionId xid;
TransactionId targetxid;
/* make sure we have a top-XID first */
topxid = GetCurrentTransactionId();
xid = ReadNewTransactionId();
targetxid = xid + nxids;
while (targetxid < FirstNormalTransactionId)
targetxid++;
while (TransactionIdPrecedes(xid, targetxid))
{
elog(DEBUG1, "xid: %u", xid);
xid = GetNewTransactionId(true);
}
PG_RETURN_VOID();
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册