Re: TupleDesc refcounting - Mailing list pgsql-patches

From Tom Lane
Subject Re: TupleDesc refcounting
Date
Msg-id 6705.1136934354@sss.pgh.pa.us
Whole thread Raw
In response to TupleDesc refcounting  (Neil Conway <neilc@samurai.com>)
Responses Re: TupleDesc refcounting  (Neil Conway <neilc@samurai.com>)
List pgsql-patches
Neil Conway <neilc@samurai.com> writes:
> (1) How should the lifetime of a TupleDesc be managed? The existing
> ResourceOwner stuff seems to assume that it is managing "per-query"
> resources.

Yeah.  I was envisioning two different approaches: for TupleDesc
references from long-lived data structures (ie, typcache or relcache)
just increment or decrement the count when the referencing data
structure changes.  ResourceOwner would be used for dynamic within-query
references --- in practice, local variables.  If the reference would
be forgotten during an elog longjmp then you need a ResourceOwner to
backstop it, otherwise not.

You could conceivably set up a "cache ResourceOwner" with indefinite
lifespan to make the cache case more like the local-variable case,
but I think this would merely be a performance drag with no real value.
There's no point in a ResourceOwner if it will never be called on to
release resources, and a cache-lifespan ResourceOwner wouldn't be.

> In current sources, the lifetime of a TupleDesc is the lifetime of the
> memory context in which it is allocated (and/or whenever FreeTupleDesc()
> is invoked). We could imitate that behavior by optionally linking a
> ResourceOwner with each MemoryContext, and releasing the ResourceOwner
> when the MemoryContext is reset or deleted. However, I'm not sure that
> that's the right approach...

No.  We really only need this mechanism for the case where a tupledesc
allocated in a long-lived context (again, think relcache or typcache)
is going to be dynamically referenced by shorter-lived code.  Tying it
to MemoryContexts won't help because CacheMemoryContext never gets
reset.

> (2) The existing ResourceOwner users issue a warning if the resource
> they are managing is not explicitly released before a transaction
> successfully commits (so they elog(WARNING)). I don't see the need to be
> that strict for TupleDescs -- as we do with palloc() without a matching
> pfree(), I think it should be okay to just silently clean up "leaked"
> TupleDescs when releasing a ResourceOwner.

The reason why those warnings are issued is to catch code that is
failing to manage references properly.  I think that motivation applies
perfectly well to tuple descriptors too.  There is no good reason for
code to forget to release the descriptor reference in non-error paths.

No time to look at the patch itself right now ...

            regards, tom lane

pgsql-patches by date:

Previous
From: Neil Conway
Date:
Subject: TupleDesc refcounting
Next
From: "Jim C. Nasby"
Date:
Subject: Re: Summary table trigger example race condition