Hello, I'm playing with creating an auto vacuum daemon, but it is my first 
time inside the pg source code and I'm a bit lost.

I have gotten as far as having a vacuum daemon created on postmaster startup.  
It's just a fork from the postmaster, cribbed mostly from the stat collector 
code. 

Inside the main loop of the autovac daemon, I am trying to call vacuum() but I 
get the following error: 
FATAL:  VACUUM cannot be executed from a function

I don't understand why it thinks I'm in a function, I believe the error is 
being generated by this is vacuum.c:

	/* Running VACUUM from a function would free the function context */
	if (vacstmt->vacuum && !MemoryContextContains(QueryContext, vacstmt))
		elog(ERROR, "%s cannot be executed from a function", stmttype);

So, I assume it has something to do with the memory context that I'm in when I 
call the vacuum command, so I have been playing with switching contexts and 
such, but I have had no luck, obviously I don't really know what is going on 
here.

The code snippet that is actually calling the vacuum looks like this:
{
	VacuumStmt *n = makeNode(VacuumStmt);
	n->vacuum = true;
	n->analyze = false;
	n->full = false;
	n->freeze = false;
	n->verbose = false;
	n->relation = NULL;
	n->va_cols = NIL;
	vacuum(n);			
}

Any help would be greatly appreciated.

Thanks,

Matt

Fri, 16 Aug 2002 00:02:02 -0400
"Matthew T. O'Connor" <matthew@zeut.net>
Coding help
"Matthew T. O'Connor" <matthew@zeut.net> writes:
> I have gotten as far as having a vacuum daemon created on postmaster startup.
> It's just a fork from the postmaster, cribbed mostly from the stat collector 
> code. 

This will not get you very far, because the stat collector is not a real
backend.  The checkpointer process might be a better example, but it's
not quite a real backend either.  You need to be a real backend to
access shared buffers, locking, etc.

> I don't understand why it thinks I'm in a function,

Because CurrentMemoryContext is not QueryContext (presumably you never
set QueryContext at all).  This is a pretty cheesy test but I can't
think of a better one offhand...

			regards, tom lane

Fri, 16 Aug 2002 01:50:21 -0400
Tom Lane <tgl@sss.pgh.pa.us>
Re: Coding help 
"Matthew T. O'Connor" wrote:
> 
> Hello, I'm playing with creating an auto vacuum daemon, but it is my first
> time inside the pg source code and I'm a bit lost.
> 
> I have gotten as far as having a vacuum daemon created on postmaster startup.
> It's just a fork from the postmaster, cribbed mostly from the stat collector
> code.

I recall that there has been discussion and so far the conclusion that
an automatic vacuum daemon is not the solution everyone needs. If you
really want to spend the effort on doing this, can we please see some
proposal about possible configuration options, how the daemon decides
what to vacuum when and the like?


Jan

-- 

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me.                                  #
#================================================== JanWieck@Yahoo.com #

Fri, 16 Aug 2002 10:10:05 -0400
Jan Wieck <JanWieck@Yahoo.com>
Re: Coding help
Absolutely, I have been looking into this and I have some thoughts, but right 
now all I was trying to do was some rough implementations just to help me 
make sure I understand all / most of the issues.  I am very new to hacking on 
the guts of the backend.  

I plan on posting a formal proposal when I feel more confident as to what I'm 
talking about.

On Friday 16 August 2002 10:10 am, Jan Wieck wrote:
> "Matthew T. O'Connor" wrote:
> > Hello, I'm playing with creating an auto vacuum daemon, but it is my
> > first time inside the pg source code and I'm a bit lost.
> >
> > I have gotten as far as having a vacuum daemon created on postmaster
> > startup. It's just a fork from the postmaster, cribbed mostly from the
> > stat collector code.
>
> I recall that there has been discussion and so far the conclusion that
> an automatic vacuum daemon is not the solution everyone needs. If you
> really want to spend the effort on doing this, can we please see some
> proposal about possible configuration options, how the daemon decides
> what to vacuum when and the like?
>
>
> Jan


Fri, 16 Aug 2002 11:25:28 -0400
"Matthew T. O'Connor" <matthew@zeut.net>
Re: Coding help