mirror of
https://github.com/postgres/postgres.git
synced 2025-06-26 12:21:12 +03:00
Introduce extensible node types.
An extensible node is always tagged T_Extensible, but the extnodename field identifies it more specifically; it may also include arbitrary private data. Extensible nodes can be copied, tested for equality, serialized, and deserialized, but the core system doesn't know anything about them otherwise. Some extensions may find it useful to include these nodes in fdw_private or custom_private lists in lieu of arm-wrestling their data into a format that the core code can understand. Along the way, so as not to burden the authors of such extensible node types too much, expose the functions for writing serialized tokens, and for serializing and deserializing bitmapsets. KaiGai Kohei, per a design suggested by me. Reviewed by Andres Freund and by me, and further edited by me.
This commit is contained in:
@ -13,7 +13,7 @@ top_builddir = ../../..
|
||||
include $(top_builddir)/src/Makefile.global
|
||||
|
||||
OBJS = nodeFuncs.o nodes.o list.o bitmapset.o tidbitmap.o \
|
||||
copyfuncs.o equalfuncs.o makefuncs.o \
|
||||
copyfuncs.o equalfuncs.o extensible.o makefuncs.o \
|
||||
outfuncs.o readfuncs.o print.o read.o params.o value.o
|
||||
|
||||
include $(top_srcdir)/src/backend/common.mk
|
||||
|
@ -23,6 +23,7 @@
|
||||
#include "postgres.h"
|
||||
|
||||
#include "miscadmin.h"
|
||||
#include "nodes/extensible.h"
|
||||
#include "nodes/plannodes.h"
|
||||
#include "nodes/relation.h"
|
||||
#include "utils/datum.h"
|
||||
@ -4165,6 +4166,27 @@ _copyList(const List *from)
|
||||
return new;
|
||||
}
|
||||
|
||||
/* ****************************************************************
|
||||
* extensible.h copy functions
|
||||
* ****************************************************************
|
||||
*/
|
||||
static ExtensibleNode *
|
||||
_copyExtensibleNode(const ExtensibleNode *from)
|
||||
{
|
||||
ExtensibleNode *newnode;
|
||||
const ExtensibleNodeMethods *methods;
|
||||
|
||||
methods = GetExtensibleNodeMethods(from->extnodename, false);
|
||||
newnode = (ExtensibleNode *) newNode(methods->node_size,
|
||||
T_ExtensibleNode);
|
||||
COPY_STRING_FIELD(extnodename);
|
||||
|
||||
/* copy the private fields */
|
||||
methods->nodeCopy(newnode, from);
|
||||
|
||||
return newnode;
|
||||
}
|
||||
|
||||
/* ****************************************************************
|
||||
* value.h copy functions
|
||||
* ****************************************************************
|
||||
@ -4544,6 +4566,13 @@ copyObject(const void *from)
|
||||
retval = list_copy(from);
|
||||
break;
|
||||
|
||||
/*
|
||||
* EXTENSIBLE NODES
|
||||
*/
|
||||
case T_ExtensibleNode:
|
||||
retval = _copyExtensibleNode(from);
|
||||
break;
|
||||
|
||||
/*
|
||||
* PARSE NODES
|
||||
*/
|
||||
|
@ -29,6 +29,7 @@
|
||||
|
||||
#include "postgres.h"
|
||||
|
||||
#include "nodes/extensible.h"
|
||||
#include "nodes/relation.h"
|
||||
#include "utils/datum.h"
|
||||
|
||||
@ -871,6 +872,25 @@ _equalPlaceHolderInfo(const PlaceHolderInfo *a, const PlaceHolderInfo *b)
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Stuff from extensible.h
|
||||
*/
|
||||
static bool
|
||||
_equalExtensibleNode(const ExtensibleNode *a, const ExtensibleNode *b)
|
||||
{
|
||||
const ExtensibleNodeMethods *methods;
|
||||
|
||||
COMPARE_STRING_FIELD(extnodename);
|
||||
|
||||
/* At this point, we know extnodename is the same for both nodes. */
|
||||
methods = GetExtensibleNodeMethods(a->extnodename, false);
|
||||
|
||||
/* compare the private fields */
|
||||
if (!methods->nodeEqual(a, b))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Stuff from parsenodes.h
|
||||
@ -2873,6 +2893,13 @@ equal(const void *a, const void *b)
|
||||
retval = _equalValue(a, b);
|
||||
break;
|
||||
|
||||
/*
|
||||
* EXTENSIBLE NODES
|
||||
*/
|
||||
case T_ExtensibleNode:
|
||||
retval = _equalExtensibleNode(a, b);
|
||||
break;
|
||||
|
||||
/*
|
||||
* PARSE NODES
|
||||
*/
|
||||
|
92
src/backend/nodes/extensible.c
Normal file
92
src/backend/nodes/extensible.c
Normal file
@ -0,0 +1,92 @@
|
||||
/*-------------------------------------------------------------------------
|
||||
*
|
||||
* extensible.c
|
||||
* Support for extensible node types
|
||||
*
|
||||
* Loadable modules can define what are in effect new types of nodes using
|
||||
* the routines in this file. All such nodes are flagged T_ExtensibleNode,
|
||||
* with the extnodename field distinguishing the specific type. Use
|
||||
* RegisterExtensibleNodeMethods to register a new type of extensible node,
|
||||
* and GetExtensibleNodeMethods to get information about a previously
|
||||
* registered type of extensible node.
|
||||
*
|
||||
* Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* src/backend/nodes/extensible.c
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
#include "postgres.h"
|
||||
|
||||
#include "nodes/extensible.h"
|
||||
#include "utils/hsearch.h"
|
||||
|
||||
static HTAB *extensible_node_methods = NULL;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char extnodename[EXTNODENAME_MAX_LEN];
|
||||
const ExtensibleNodeMethods *methods;
|
||||
} ExtensibleNodeEntry;
|
||||
|
||||
/*
|
||||
* Register a new type of extensible node.
|
||||
*/
|
||||
void
|
||||
RegisterExtensibleNodeMethods(const ExtensibleNodeMethods *methods)
|
||||
{
|
||||
ExtensibleNodeEntry *entry;
|
||||
bool found;
|
||||
|
||||
if (extensible_node_methods == NULL)
|
||||
{
|
||||
HASHCTL ctl;
|
||||
|
||||
memset(&ctl, 0, sizeof(HASHCTL));
|
||||
ctl.keysize = NAMEDATALEN;
|
||||
ctl.entrysize = sizeof(ExtensibleNodeEntry);
|
||||
extensible_node_methods = hash_create("Extensible Node Methods",
|
||||
100, &ctl, HASH_ELEM);
|
||||
}
|
||||
|
||||
Assert(strlen(methods->extnodename) <= EXTNODENAME_MAX_LEN);
|
||||
|
||||
entry = (ExtensibleNodeEntry *) hash_search(extensible_node_methods,
|
||||
methods->extnodename,
|
||||
HASH_ENTER, &found);
|
||||
if (found)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_DUPLICATE_OBJECT),
|
||||
errmsg("extensible node type \"%s\" already exists",
|
||||
methods->extnodename)));
|
||||
|
||||
entry->methods = methods;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the methods for a given type of extensible node.
|
||||
*/
|
||||
const ExtensibleNodeMethods *
|
||||
GetExtensibleNodeMethods(const char *extnodename, bool missing_ok)
|
||||
{
|
||||
ExtensibleNodeEntry *entry = NULL;
|
||||
|
||||
if (extensible_node_methods != NULL)
|
||||
entry = (ExtensibleNodeEntry *) hash_search(extensible_node_methods,
|
||||
extnodename,
|
||||
HASH_FIND, NULL);
|
||||
|
||||
if (!entry)
|
||||
{
|
||||
if (missing_ok)
|
||||
return NULL;
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
||||
errmsg("ExtensibleNodeMethods \"%s\" was not registered",
|
||||
extnodename)));
|
||||
}
|
||||
|
||||
return entry->methods;
|
||||
}
|
@ -26,6 +26,7 @@
|
||||
#include <ctype.h>
|
||||
|
||||
#include "lib/stringinfo.h"
|
||||
#include "nodes/extensible.h"
|
||||
#include "nodes/plannodes.h"
|
||||
#include "nodes/relation.h"
|
||||
#include "utils/datum.h"
|
||||
@ -140,6 +141,13 @@ _outToken(StringInfo str, const char *s)
|
||||
}
|
||||
}
|
||||
|
||||
/* for use by extensions which define extensible nodes */
|
||||
void
|
||||
outToken(StringInfo str, const char *s)
|
||||
{
|
||||
_outToken(str, s);
|
||||
}
|
||||
|
||||
static void
|
||||
_outList(StringInfo str, const List *node)
|
||||
{
|
||||
@ -196,6 +204,13 @@ _outBitmapset(StringInfo str, const Bitmapset *bms)
|
||||
appendStringInfoChar(str, ')');
|
||||
}
|
||||
|
||||
/* for use by extensions which define extensible nodes */
|
||||
void
|
||||
outBitmapset(StringInfo str, const Bitmapset *bms)
|
||||
{
|
||||
_outBitmapset(str, bms);
|
||||
}
|
||||
|
||||
/*
|
||||
* Print the value of a Datum given its type.
|
||||
*/
|
||||
@ -2112,6 +2127,27 @@ _outPlannerParamItem(StringInfo str, const PlannerParamItem *node)
|
||||
WRITE_INT_FIELD(paramId);
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
*
|
||||
* Stuff from extensible.h
|
||||
*
|
||||
*****************************************************************************/
|
||||
|
||||
static void
|
||||
_outExtensibleNode(StringInfo str, const ExtensibleNode *node)
|
||||
{
|
||||
const ExtensibleNodeMethods *methods;
|
||||
|
||||
methods = GetExtensibleNodeMethods(node->extnodename, false);
|
||||
|
||||
WRITE_NODE_TYPE("EXTENSIBLENODE");
|
||||
|
||||
WRITE_STRING_FIELD(extnodename);
|
||||
|
||||
/* serialize the private fields */
|
||||
methods->nodeOut(str, node);
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
*
|
||||
* Stuff from parsenodes.h.
|
||||
@ -3367,6 +3403,10 @@ _outNode(StringInfo str, const void *obj)
|
||||
_outPlannerParamItem(str, obj);
|
||||
break;
|
||||
|
||||
case T_ExtensibleNode:
|
||||
_outExtensibleNode(str, obj);
|
||||
break;
|
||||
|
||||
case T_CreateStmt:
|
||||
_outCreateStmt(str, obj);
|
||||
break;
|
||||
|
@ -29,6 +29,7 @@
|
||||
#include <math.h>
|
||||
|
||||
#include "fmgr.h"
|
||||
#include "nodes/extensible.h"
|
||||
#include "nodes/parsenodes.h"
|
||||
#include "nodes/plannodes.h"
|
||||
#include "nodes/readfuncs.h"
|
||||
@ -218,6 +219,14 @@ _readBitmapset(void)
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* for use by extensions which define extensible nodes
|
||||
*/
|
||||
Bitmapset *
|
||||
readBitmapset(void)
|
||||
{
|
||||
return _readBitmapset();
|
||||
}
|
||||
|
||||
/*
|
||||
* _readQuery
|
||||
@ -2219,6 +2228,35 @@ _readAlternativeSubPlan(void)
|
||||
READ_DONE();
|
||||
}
|
||||
|
||||
/*
|
||||
* _readExtensibleNode
|
||||
*/
|
||||
static ExtensibleNode *
|
||||
_readExtensibleNode(void)
|
||||
{
|
||||
const ExtensibleNodeMethods *methods;
|
||||
ExtensibleNode *local_node;
|
||||
const char *extnodename;
|
||||
READ_TEMP_LOCALS();
|
||||
|
||||
token = pg_strtok(&length); /* skip: extnodename */
|
||||
token = pg_strtok(&length); /* get extnodename */
|
||||
|
||||
extnodename = nullable_string(token, length);
|
||||
if (!extnodename)
|
||||
elog(ERROR, "extnodename has to be supplied");
|
||||
methods = GetExtensibleNodeMethods(extnodename, false);
|
||||
|
||||
local_node = (ExtensibleNode *) newNode(methods->node_size,
|
||||
T_ExtensibleNode);
|
||||
local_node->extnodename = extnodename;
|
||||
|
||||
/* deserialize the private fields */
|
||||
methods->nodeRead(local_node);
|
||||
|
||||
READ_DONE();
|
||||
}
|
||||
|
||||
/*
|
||||
* parseNodeString
|
||||
*
|
||||
@ -2447,6 +2485,8 @@ parseNodeString(void)
|
||||
return_value = _readSubPlan();
|
||||
else if (MATCH("ALTERNATIVESUBPLAN", 18))
|
||||
return_value = _readAlternativeSubPlan();
|
||||
else if (MATCH("EXTENSIBLENODE", 14))
|
||||
return_value = _readExtensibleNode();
|
||||
else
|
||||
{
|
||||
elog(ERROR, "badly formatted node string \"%.32s\"...", token);
|
||||
|
Reference in New Issue
Block a user