mirror of
https://github.com/postgres/postgres.git
synced 2025-07-07 00:36:50 +03:00
Allow memory contexts to have both fixed and variable ident strings.
Originally, we treated memory context names as potentially variable in all cases, and therefore always copied them into the context header. Commit9fa6f00b1
rethought this a little bit and invented a distinction between fixed and variable names, skipping the copy step for the former. But we can make things both simpler and more useful by instead allowing there to be two parts to a context's identification, a fixed "name" and an optional, variable "ident". The name supplied in the context create call is now required to be a compile-time-constant string in all cases, as it is never copied but just pointed to. The "ident" string, if wanted, is supplied later. This is needed because typically we want the ident to be stored inside the context so that it's cleaned up automatically on context deletion; that means it has to be copied into the context before we can set the pointer. The cost of this approach is basically just an additional pointer field in struct MemoryContextData, which isn't much overhead, and is bought back entirely in the AllocSet case by not needing a headerSize field anymore, since we no longer have to cope with variable header length. In addition, we can simplify the internal interfaces for memory context creation still further, saving a few cycles there. And it's no longer true that a custom identifier disqualifies a context from participating in aset.c's freelist scheme, so possibly there's some win on that end. All the places that were using non-compile-time-constant context names are adjusted to put the variable info into the "ident" instead. This allows more effective identification of those contexts in many cases; for example, subsidary contexts of relcache entries are now identified by both type (e.g. "index info") and relname, where before you got only one or the other. Contexts associated with PL function cache entries are now identified more fully and uniformly, too. I also arranged for plancache contexts to use the query source string as their identifier. This is basically free for CachedPlanSources, as they contained a copy of that string already. We pay an extra pstrdup to do it for CachedPlans. That could perhaps be avoided, but it would make things more fragile (since the CachedPlanSource is sometimes destroyed first). I suspect future improvements in error reporting will require CachedPlans to have a copy of that string anyway, so it's not clear that it's worth moving mountains to avoid it now. This also changes the APIs for context statistics routines so that the context-specific routines no longer assume that output goes straight to stderr, nor do they know all details of the output format. This is useful immediately to reduce code duplication, and it also allows for external code to do something with stats output that's different from printing to stderr. The reason for pushing this now rather than waiting for v12 is that it rethinks some of the API changes made by commit9fa6f00b1
. Seems better for extension authors to endure just one round of API changes not two. Discussion: https://postgr.es/m/CAB=Je-FdtmFZ9y9REHD7VsSrnCkiBhsA4mdsLKSPauwXtQBeNA@mail.gmail.com
This commit is contained in:
@ -76,6 +76,7 @@ extern void MemoryContextDelete(MemoryContext context);
|
||||
extern void MemoryContextResetOnly(MemoryContext context);
|
||||
extern void MemoryContextResetChildren(MemoryContext context);
|
||||
extern void MemoryContextDeleteChildren(MemoryContext context);
|
||||
extern void MemoryContextSetIdentifier(MemoryContext context, const char *id);
|
||||
extern void MemoryContextSetParent(MemoryContext context,
|
||||
MemoryContext new_parent);
|
||||
extern Size GetMemoryChunkSpace(void *pointer);
|
||||
@ -91,6 +92,10 @@ extern void MemoryContextCheck(MemoryContext context);
|
||||
#endif
|
||||
extern bool MemoryContextContains(MemoryContext context, void *pointer);
|
||||
|
||||
/* Handy macro for copying and assigning context ID ... but note double eval */
|
||||
#define MemoryContextCopySetIdentifier(cxt, id) \
|
||||
MemoryContextSetIdentifier(cxt, MemoryContextStrdup(cxt, id))
|
||||
|
||||
/*
|
||||
* GetMemoryChunkContext
|
||||
* Given a currently-allocated chunk, determine the context
|
||||
@ -133,11 +138,10 @@ GetMemoryChunkContext(void *pointer)
|
||||
* specific creation routines, and noplace else.
|
||||
*/
|
||||
extern void MemoryContextCreate(MemoryContext node,
|
||||
NodeTag tag, Size size, Size nameoffset,
|
||||
NodeTag tag,
|
||||
const MemoryContextMethods *methods,
|
||||
MemoryContext parent,
|
||||
const char *name,
|
||||
int flags);
|
||||
const char *name);
|
||||
|
||||
|
||||
/*
|
||||
@ -147,46 +151,37 @@ extern void MemoryContextCreate(MemoryContext node,
|
||||
/* aset.c */
|
||||
extern MemoryContext AllocSetContextCreateExtended(MemoryContext parent,
|
||||
const char *name,
|
||||
int flags,
|
||||
Size minContextSize,
|
||||
Size initBlockSize,
|
||||
Size maxBlockSize);
|
||||
|
||||
/*
|
||||
* This backwards compatibility macro only works for constant context names,
|
||||
* and you must specify block sizes with one of the abstraction macros below.
|
||||
* This wrapper macro exists to check for non-constant strings used as context
|
||||
* names; that's no longer supported. (Use MemoryContextSetIdentifier if you
|
||||
* want to provide a variable identifier.) Note you must specify block sizes
|
||||
* with one of the abstraction macros below.
|
||||
*/
|
||||
#ifdef HAVE__BUILTIN_CONSTANT_P
|
||||
#define AllocSetContextCreate(parent, name, allocparams) \
|
||||
(StaticAssertExpr(__builtin_constant_p(name), \
|
||||
"Use AllocSetContextCreateExtended with MEMCONTEXT_COPY_NAME for non-constant context names"), \
|
||||
AllocSetContextCreateExtended(parent, name, 0, allocparams))
|
||||
"memory context names must be constant strings"), \
|
||||
AllocSetContextCreateExtended(parent, name, allocparams))
|
||||
#else
|
||||
#define AllocSetContextCreate(parent, name, allocparams) \
|
||||
AllocSetContextCreateExtended(parent, name, 0, allocparams)
|
||||
AllocSetContextCreateExtended(parent, name, allocparams)
|
||||
#endif
|
||||
|
||||
/* slab.c */
|
||||
extern MemoryContext SlabContextCreate(MemoryContext parent,
|
||||
const char *name,
|
||||
int flags,
|
||||
Size blockSize,
|
||||
Size chunkSize);
|
||||
|
||||
/* generation.c */
|
||||
extern MemoryContext GenerationContextCreate(MemoryContext parent,
|
||||
const char *name,
|
||||
int flags,
|
||||
Size blockSize);
|
||||
|
||||
/*
|
||||
* Flag option bits for FooContextCreate functions.
|
||||
* In future, some of these might be relevant to only some context types.
|
||||
*
|
||||
* COPY_NAME: FooContextCreate's name argument is not a constant string
|
||||
*/
|
||||
#define MEMCONTEXT_COPY_NAME 0x0001 /* is passed name transient? */
|
||||
|
||||
/*
|
||||
* Recommended default alloc parameters, suitable for "ordinary" contexts
|
||||
* that might hold quite a lot of data.
|
||||
|
Reference in New Issue
Block a user