diff options
author | Alvaro Herrera <alvherre@alvh.no-ip.org> | 2020-03-02 18:19:51 -0300 |
---|---|---|
committer | Alvaro Herrera <alvherre@alvh.no-ip.org> | 2020-03-02 18:19:51 -0300 |
commit | 2f9661311b83dc481fc19f6e3bda015392010a40 (patch) | |
tree | 9a1aabe1d15ac894f7badbc886ae33f16bbfc3b6 /src/backend/utils/cache | |
parent | 7b425a5283cb2c8a452c2e79d6218e41373fd641 (diff) | |
download | postgresql-2f9661311b83dc481fc19f6e3bda015392010a40.tar.gz postgresql-2f9661311b83dc481fc19f6e3bda015392010a40.zip |
Represent command completion tags as structs
The backend was using strings to represent command tags and doing string
comparisons in multiple places, but that's slow and unhelpful. Create a
new command list with a supporting structure to use instead; this is
stored in a tag-list-file that can be tailored to specific purposes with
a caller-definable C macro, similar to what we do for WAL resource
managers. The first first such uses are a new CommandTag enum and a
CommandTagBehavior struct.
Replace numerous occurrences of char *completionTag with a
QueryCompletion struct so that the code no longer stores information
about completed queries in a cstring. Only at the last moment, in
EndCommand(), does this get converted to a string.
EventTriggerCacheItem no longer holds an array of palloc’d tag strings
in sorted order, but rather just a Bitmapset over the CommandTags.
Author: Mark Dilger, with unsolicited help from Álvaro Herrera
Reviewed-by: John Naylor, Tom Lane
Discussion: https://postgr.es/m/981A9DB4-3F0C-4DA5-88AD-CB9CFF4D6CAD@enterprisedb.com
Diffstat (limited to 'src/backend/utils/cache')
-rw-r--r-- | src/backend/utils/cache/evtcache.c | 30 | ||||
-rw-r--r-- | src/backend/utils/cache/plancache.c | 8 |
2 files changed, 20 insertions, 18 deletions
diff --git a/src/backend/utils/cache/evtcache.c b/src/backend/utils/cache/evtcache.c index 1b63048a774..b9c1a0a5adb 100644 --- a/src/backend/utils/cache/evtcache.c +++ b/src/backend/utils/cache/evtcache.c @@ -20,6 +20,7 @@ #include "catalog/pg_event_trigger.h" #include "catalog/pg_type.h" #include "commands/trigger.h" +#include "tcop/cmdtag.h" #include "utils/array.h" #include "utils/builtins.h" #include "utils/catcache.h" @@ -51,7 +52,7 @@ static EventTriggerCacheStateType EventTriggerCacheState = ETCS_NEEDS_REBUILD; static void BuildEventTriggerCache(void); static void InvalidateEventCacheCallback(Datum arg, int cacheid, uint32 hashvalue); -static int DecodeTextArrayToCString(Datum array, char ***cstringp); +static Bitmapset *DecodeTextArrayToBitmapset(Datum array); /* * Search the event cache by trigger event. @@ -180,10 +181,7 @@ BuildEventTriggerCache(void) evttags = heap_getattr(tup, Anum_pg_event_trigger_evttags, RelationGetDescr(rel), &evttags_isnull); if (!evttags_isnull) - { - item->ntags = DecodeTextArrayToCString(evttags, &item->tag); - qsort(item->tag, item->ntags, sizeof(char *), pg_qsort_strcmp); - } + item->tagset = DecodeTextArrayToBitmapset(evttags); /* Add to cache entry. */ entry = hash_search(cache, &event, HASH_ENTER, &found); @@ -215,18 +213,18 @@ BuildEventTriggerCache(void) } /* - * Decode text[] to an array of C strings. + * Decode text[] to a Bitmapset of CommandTags. * * We could avoid a bit of overhead here if we were willing to duplicate some * of the logic from deconstruct_array, but it doesn't seem worth the code * complexity. */ -static int -DecodeTextArrayToCString(Datum array, char ***cstringp) +static Bitmapset * +DecodeTextArrayToBitmapset(Datum array) { ArrayType *arr = DatumGetArrayTypeP(array); Datum *elems; - char **cstring; + Bitmapset *bms; int i; int nelems; @@ -234,13 +232,17 @@ DecodeTextArrayToCString(Datum array, char ***cstringp) elog(ERROR, "expected 1-D text array"); deconstruct_array(arr, TEXTOID, -1, false, 'i', &elems, NULL, &nelems); - cstring = palloc(nelems * sizeof(char *)); - for (i = 0; i < nelems; ++i) - cstring[i] = TextDatumGetCString(elems[i]); + for (bms = NULL, i = 0; i < nelems; ++i) + { + char *str = TextDatumGetCString(elems[i]); + + bms = bms_add_member(bms, GetCommandTagEnum(str)); + pfree(str); + } pfree(elems); - *cstringp = cstring; - return nelems; + + return bms; } /* diff --git a/src/backend/utils/cache/plancache.c b/src/backend/utils/cache/plancache.c index c47be0ba4cd..dbae18d68c6 100644 --- a/src/backend/utils/cache/plancache.c +++ b/src/backend/utils/cache/plancache.c @@ -158,12 +158,12 @@ InitPlanCache(void) * * raw_parse_tree: output of raw_parser(), or NULL if empty query * query_string: original query text - * commandTag: compile-time-constant tag for query, or NULL if empty query + * commandTag: command tag for query, or UNKNOWN if empty query */ CachedPlanSource * CreateCachedPlan(RawStmt *raw_parse_tree, const char *query_string, - const char *commandTag) + CommandTag commandTag) { CachedPlanSource *plansource; MemoryContext source_context; @@ -241,12 +241,12 @@ CreateCachedPlan(RawStmt *raw_parse_tree, * * raw_parse_tree: output of raw_parser(), or NULL if empty query * query_string: original query text - * commandTag: compile-time-constant tag for query, or NULL if empty query + * commandTag: command tag for query, or NULL if empty query */ CachedPlanSource * CreateOneShotCachedPlan(RawStmt *raw_parse_tree, const char *query_string, - const char *commandTag) + CommandTag commandTag) { CachedPlanSource *plansource; |