Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf9143d1

Browse files
committed
Rework custom scans to work more like the new extensible node stuff.
Per discussion, the new extensible node framework is thought to bebetter designed than the custom path/scan/scanstate stuff we addedin PostgreSQL 9.5. Rework the latter to be more like the former.This is not backward-compatible, but we generally don't promise thatfor C APIs, and there probably aren't many people using this yetanyway.KaiGai Kohei, reviewed by Petr Jelinek and me. Some furthercosmetic changes by me.
1 parent534da37 commitf9143d1

File tree

10 files changed

+168
-101
lines changed

10 files changed

+168
-101
lines changed

‎src/backend/commands/explain.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include"commands/prepare.h"
2222
#include"executor/hashjoin.h"
2323
#include"foreign/fdwapi.h"
24+
#include"nodes/extensible.h"
2425
#include"nodes/nodeFuncs.h"
2526
#include"optimizer/clauses.h"
2627
#include"optimizer/planmain.h"

‎src/backend/nodes/extensible.c

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,61 +24,87 @@
2424
#include"utils/hsearch.h"
2525

2626
staticHTAB*extensible_node_methods=NULL;
27+
staticHTAB*custom_scan_methods=NULL;
2728

2829
typedefstruct
2930
{
3031
charextnodename[EXTNODENAME_MAX_LEN];
31-
constExtensibleNodeMethods*methods;
32+
constvoid*extnodemethods;
3233
}ExtensibleNodeEntry;
3334

3435
/*
35-
*Registera newtype of extensible node.
36+
*An internal function to registera newcallback structure
3637
*/
37-
void
38-
RegisterExtensibleNodeMethods(constExtensibleNodeMethods*methods)
38+
staticvoid
39+
RegisterExtensibleNodeEntry(HTAB**p_htable,constchar*htable_label,
40+
constchar*extnodename,
41+
constvoid*extnodemethods)
3942
{
4043
ExtensibleNodeEntry*entry;
4144
boolfound;
4245

43-
if (extensible_node_methods==NULL)
46+
if (*p_htable==NULL)
4447
{
4548
HASHCTLctl;
4649

4750
memset(&ctl,0,sizeof(HASHCTL));
4851
ctl.keysize=EXTNODENAME_MAX_LEN;
4952
ctl.entrysize=sizeof(ExtensibleNodeEntry);
50-
extensible_node_methods=hash_create("Extensible Node Methods",
51-
100,&ctl,HASH_ELEM);
53+
54+
*p_htable=hash_create(htable_label,100,&ctl,HASH_ELEM);
5255
}
5356

54-
if (strlen(methods->extnodename) >=EXTNODENAME_MAX_LEN)
57+
if (strlen(extnodename) >=EXTNODENAME_MAX_LEN)
5558
elog(ERROR,"extensible node name is too long");
5659

57-
entry= (ExtensibleNodeEntry*)hash_search(extensible_node_methods,
58-
methods->extnodename,
60+
entry= (ExtensibleNodeEntry*)hash_search(*p_htable,
61+
extnodename,
5962
HASH_ENTER,&found);
6063
if (found)
6164
ereport(ERROR,
6265
(errcode(ERRCODE_DUPLICATE_OBJECT),
6366
errmsg("extensible node type \"%s\" already exists",
64-
methods->extnodename)));
67+
extnodename)));
6568

66-
entry->methods=methods;
69+
entry->extnodemethods=extnodemethods;
6770
}
6871

6972
/*
70-
*Get the methods for a given type of extensible node.
73+
*Register a new type of extensible node.
7174
*/
72-
constExtensibleNodeMethods*
73-
GetExtensibleNodeMethods(constchar*extnodename,boolmissing_ok)
75+
void
76+
RegisterExtensibleNodeMethods(constExtensibleNodeMethods*methods)
77+
{
78+
RegisterExtensibleNodeEntry(&extensible_node_methods,
79+
"Extensible Node Methods",
80+
methods->extnodename,
81+
methods);
82+
}
83+
84+
/*
85+
* Register a new type of custom scan node
86+
*/
87+
void
88+
RegisterCustomScanMethods(constCustomScanMethods*methods)
89+
{
90+
RegisterExtensibleNodeEntry(&custom_scan_methods,
91+
"Custom Scan Methods",
92+
methods->CustomName,
93+
methods);
94+
}
95+
96+
/*
97+
* An internal routine to get an ExtensibleNodeEntry by the given identifier
98+
*/
99+
staticconstvoid*
100+
GetExtensibleNodeEntry(HTAB*htable,constchar*extnodename,boolmissing_ok)
74101
{
75102
ExtensibleNodeEntry*entry=NULL;
76103

77-
if (extensible_node_methods!=NULL)
78-
entry= (ExtensibleNodeEntry*)hash_search(extensible_node_methods,
104+
if (htable!=NULL)
105+
entry= (ExtensibleNodeEntry*)hash_search(htable,
79106
extnodename,
80107
HASH_FIND,NULL);
81-
82108
if (!entry)
83109
{
84110
if (missing_ok)
@@ -89,5 +115,29 @@ GetExtensibleNodeMethods(const char *extnodename, bool missing_ok)
89115
extnodename)));
90116
}
91117

92-
returnentry->methods;
118+
returnentry->extnodemethods;
119+
}
120+
121+
/*
122+
* Get the methods for a given type of extensible node.
123+
*/
124+
constExtensibleNodeMethods*
125+
GetExtensibleNodeMethods(constchar*extnodename,boolmissing_ok)
126+
{
127+
return (constExtensibleNodeMethods*)
128+
GetExtensibleNodeEntry(extensible_node_methods,
129+
extnodename,
130+
missing_ok);
131+
}
132+
133+
/*
134+
* Get the methods for a given name of CustomScanMethods
135+
*/
136+
constCustomScanMethods*
137+
GetCustomScanMethods(constchar*CustomName,boolmissing_ok)
138+
{
139+
return (constCustomScanMethods*)
140+
GetExtensibleNodeEntry(custom_scan_methods,
141+
CustomName,
142+
missing_ok);
93143
}

‎src/backend/nodes/outfuncs.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -632,11 +632,9 @@ _outCustomScan(StringInfo str, const CustomScan *node)
632632
WRITE_NODE_FIELD(custom_private);
633633
WRITE_NODE_FIELD(custom_scan_tlist);
634634
WRITE_BITMAPSET_FIELD(custom_relids);
635-
/*Dump library and symbol name instead of raw pointer */
635+
/*CustomName is a key to lookup CustomScanMethods */
636636
appendStringInfoString(str," :methods ");
637-
_outToken(str,node->methods->LibraryName);
638-
appendStringInfoChar(str,' ');
639-
_outToken(str,node->methods->SymbolName);
637+
_outToken(str,node->methods->CustomName);
640638
}
641639

642640
staticvoid

‎src/backend/nodes/readfuncs.c

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,8 +1827,7 @@ static CustomScan *
18271827
_readCustomScan(void)
18281828
{
18291829
READ_LOCALS(CustomScan);
1830-
char*library_name;
1831-
char*symbol_name;
1830+
char*custom_name;
18321831
constCustomScanMethods*methods;
18331832

18341833
ReadCommonScan(&local_node->scan);
@@ -1840,19 +1839,11 @@ _readCustomScan(void)
18401839
READ_NODE_FIELD(custom_scan_tlist);
18411840
READ_BITMAPSET_FIELD(custom_relids);
18421841

1843-
/*
1844-
* Reconstruction of methods using library and symbol name
1845-
*/
1842+
/* Lookup CustomScanMethods by CustomName */
18461843
token=pg_strtok(&length);/* skip methods: */
1847-
token=pg_strtok(&length);/* LibraryName */
1848-
library_name=nullable_string(token,length);
1849-
token=pg_strtok(&length);/* SymbolName */
1850-
symbol_name=nullable_string(token,length);
1851-
1852-
methods= (constCustomScanMethods*)
1853-
load_external_function(library_name,symbol_name, true,NULL);
1854-
Assert(strcmp(methods->LibraryName,library_name)==0&&
1855-
strcmp(methods->SymbolName,symbol_name)==0);
1844+
token=pg_strtok(&length);/* CustomName */
1845+
custom_name=nullable_string(token,length);
1846+
methods=GetCustomScanMethods(custom_name, false);
18561847
local_node->methods=methods;
18571848

18581849
READ_DONE();

‎src/backend/optimizer/plan/createplan.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include"catalog/pg_class.h"
2525
#include"foreign/fdwapi.h"
2626
#include"miscadmin.h"
27+
#include"nodes/extensible.h"
2728
#include"nodes/makefuncs.h"
2829
#include"nodes/nodeFuncs.h"
2930
#include"optimizer/clauses.h"

‎src/include/executor/nodeCustom.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include"access/parallel.h"
1616
#include"nodes/execnodes.h"
17+
#include"nodes/extensible.h"
1718

1819
/*
1920
* General executor code

‎src/include/nodes/execnodes.h

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,46 +1606,15 @@ typedef struct ForeignScanState
16061606
* the BeginCustomScan method.
16071607
* ----------------
16081608
*/
1609-
structParallelContext;/* avoid including parallel.h here */
1610-
structshm_toc;/* avoid including shm_toc.h here */
1611-
structExplainState;/* avoid including explain.h here */
1612-
structCustomScanState;
1613-
1614-
typedefstructCustomExecMethods
1615-
{
1616-
constchar*CustomName;
1617-
1618-
/* Executor methods: mark/restore are optional, the rest are required */
1619-
void(*BeginCustomScan) (structCustomScanState*node,
1620-
EState*estate,
1621-
inteflags);
1622-
TupleTableSlot*(*ExecCustomScan) (structCustomScanState*node);
1623-
void(*EndCustomScan) (structCustomScanState*node);
1624-
void(*ReScanCustomScan) (structCustomScanState*node);
1625-
void(*MarkPosCustomScan) (structCustomScanState*node);
1626-
void(*RestrPosCustomScan) (structCustomScanState*node);
1627-
/* Optional: parallel execution support */
1628-
Size(*EstimateDSMCustomScan) (structCustomScanState*node,
1629-
structParallelContext*pcxt);
1630-
void(*InitializeDSMCustomScan) (structCustomScanState*node,
1631-
structParallelContext*pcxt,
1632-
void*coordinate);
1633-
void(*InitializeWorkerCustomScan) (structCustomScanState*node,
1634-
structshm_toc*toc,
1635-
void*coordinate);
1636-
/* Optional: print additional information in EXPLAIN */
1637-
void(*ExplainCustomScan) (structCustomScanState*node,
1638-
List*ancestors,
1639-
structExplainState*es);
1640-
}CustomExecMethods;
1609+
structCustomExecMethods;
16411610

16421611
typedefstructCustomScanState
16431612
{
16441613
ScanStatess;
16451614
uint32flags;/* mask of CUSTOMPATH_* flags, see relation.h */
16461615
List*custom_ps;/* list of child PlanState nodes, if any */
16471616
Sizepscan_len;/* size of parallel coordination information */
1648-
constCustomExecMethods*methods;
1617+
conststructCustomExecMethods*methods;
16491618
}CustomScanState;
16501619

16511620
/* ----------------------------------------------------------------

‎src/include/nodes/extensible.h

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*-------------------------------------------------------------------------
22
*
33
* extensible.h
4-
* Definitions for extensiblenode type
4+
* Definitions for extensiblenodes and custom scans
55
*
66
*
77
* Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
@@ -14,8 +14,13 @@
1414
#ifndefEXTENSIBLE_H
1515
#defineEXTENSIBLE_H
1616

17-
#include"nodes/nodes.h"
17+
#include"access/parallel.h"
18+
#include"commands/explain.h"
19+
#include"nodes/execnodes.h"
20+
#include"nodes/plannodes.h"
21+
#include"nodes/relation.h"
1822

23+
/* maximum length of an extensible node identifier */
1924
#defineEXTNODENAME_MAX_LEN64
2025

2126
/*
@@ -69,4 +74,80 @@ extern void RegisterExtensibleNodeMethods(const ExtensibleNodeMethods *method);
6974
externconstExtensibleNodeMethods*GetExtensibleNodeMethods(constchar*name,
7075
boolmissing_ok);
7176

77+
/*
78+
* Flags for custom paths, indicating what capabilities the resulting scan
79+
* will have.
80+
*/
81+
#defineCUSTOMPATH_SUPPORT_BACKWARD_SCAN0x0001
82+
#defineCUSTOMPATH_SUPPORT_MARK_RESTORE0x0002
83+
84+
/*
85+
* Custom path methods. Mostly, we just need to know how to convert a
86+
* CustomPath to a plan.
87+
*/
88+
typedefstructCustomPathMethods
89+
{
90+
constchar*CustomName;
91+
92+
/* Convert Path to a Plan */
93+
structPlan*(*PlanCustomPath) (PlannerInfo*root,
94+
RelOptInfo*rel,
95+
structCustomPath*best_path,
96+
List*tlist,
97+
List*clauses,
98+
List*custom_plans);
99+
}CustomPathMethods;
100+
101+
/*
102+
* Custom scan. Here again, there's not much to do: we need to be able to
103+
* generate a ScanState corresponding to the scan.
104+
*/
105+
typedefstructCustomScanMethods
106+
{
107+
constchar*CustomName;
108+
109+
/* Create execution state (CustomScanState) from a CustomScan plan node */
110+
Node*(*CreateCustomScanState) (CustomScan*cscan);
111+
}CustomScanMethods;
112+
113+
/*
114+
* Execution-time methods for a CustomScanState. This is more complex than
115+
* what we need for a custom path or scan.
116+
*/
117+
typedefstructCustomExecMethods
118+
{
119+
constchar*CustomName;
120+
121+
/* Required executor methods */
122+
void(*BeginCustomScan) (CustomScanState*node,
123+
EState*estate,
124+
inteflags);
125+
TupleTableSlot*(*ExecCustomScan) (CustomScanState*node);
126+
void(*EndCustomScan) (CustomScanState*node);
127+
void(*ReScanCustomScan) (CustomScanState*node);
128+
129+
/* Optional methods: needed if mark/restore is supported */
130+
void(*MarkPosCustomScan) (CustomScanState*node);
131+
void(*RestrPosCustomScan) (CustomScanState*node);
132+
133+
/* Optional methods: needed if parallel execution is supported */
134+
Size(*EstimateDSMCustomScan) (CustomScanState*node,
135+
ParallelContext*pcxt);
136+
void(*InitializeDSMCustomScan) (CustomScanState*node,
137+
ParallelContext*pcxt,
138+
void*coordinate);
139+
void(*InitializeWorkerCustomScan) (CustomScanState*node,
140+
shm_toc*toc,
141+
void*coordinate);
142+
143+
/* Optional: print additional information in EXPLAIN */
144+
void(*ExplainCustomScan) (CustomScanState*node,
145+
List*ancestors,
146+
ExplainState*es);
147+
}CustomExecMethods;
148+
149+
externvoidRegisterCustomScanMethods(constCustomScanMethods*methods);
150+
externconstCustomScanMethods*GetCustomScanMethods(constchar*CustomName,
151+
boolmissing_ok);
152+
72153
#endif/* EXTENSIBLE_H */

‎src/include/nodes/plannodes.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -555,17 +555,7 @@ typedef struct ForeignScan
555555
* a larger struct will not work.
556556
* ----------------
557557
*/
558-
structCustomScan;
559-
560-
typedefstructCustomScanMethods
561-
{
562-
constchar*CustomName;
563-
constchar*LibraryName;
564-
constchar*SymbolName;
565-
566-
/* Create execution state (CustomScanState) from a CustomScan plan node */
567-
Node*(*CreateCustomScanState) (structCustomScan*cscan);
568-
}CustomScanMethods;
558+
structCustomScanMethods;
569559

570560
typedefstructCustomScan
571561
{
@@ -577,7 +567,7 @@ typedef struct CustomScan
577567
List*custom_scan_tlist;/* optional tlist describing scan
578568
* tuple */
579569
Bitmapset*custom_relids;/* RTIs generated by this scan */
580-
constCustomScanMethods*methods;
570+
conststructCustomScanMethods*methods;
581571
}CustomScan;
582572

583573
/*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp