/*

build this into a Postgres shared library, then

create or replace function drive_builtin_by_name(funcname text, count int) returns int
strict volatile language c as '$libdir/drive_builtin_by_name.so';

select drive_parser('some-function', 100000);

\timing

select drive_builtin_by_name('some-function', 100000);

 */

#include "postgres.h"

#include "fmgr.h"
#include "miscadmin.h"
#include "utils/builtins.h"
#include "utils/fmgrtab.h"

PG_MODULE_MAGIC;

/*
 * drive_builtin_by_name(funcname text, count int) returns int
 */
PG_FUNCTION_INFO_V1(drive_builtin_by_name);
Datum
drive_builtin_by_name(PG_FUNCTION_ARGS)
{
	text	   *txt = PG_GETARG_TEXT_PP(0);
	int32		count = PG_GETARG_INT32(1);
	char	   *func_name = text_to_cstring(txt);
	Oid			foid;

	while (count-- > 0)
	{

		foid = fmgr_internal_function(func_name);
		CHECK_FOR_INTERRUPTS();
	}

	PG_RETURN_INT32(foid);
}
