/*

create function drive_palloc(count int) returns void
strict volatile language c as '.../drive_palloc.so';

\timing

select drive_palloc(10000000);

 */

#include "postgres.h"

#include "fmgr.h"
#include "miscadmin.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
#include "utils/memutils.h"

PG_MODULE_MAGIC;

/*
 * drive_palloc(count int) returns void
 */
PG_FUNCTION_INFO_V1(drive_palloc);
Datum
drive_palloc(PG_FUNCTION_ARGS)
{
	int32		count = PG_GETARG_INT32(0);

	while (count-- > 0)
	{
		for (size_t sz = 1; sz <= 8192; sz <<= 1)
		{
			void *p = palloc(sz);
			pfree(p);
		}

		CHECK_FOR_INTERRUPTS();
	}

	PG_RETURN_VOID();
}
