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

Commit1908abc

Browse files
committed
Arrange to cache FdwRoutine structs in foreign tables' relcache entries.
This saves several catalog lookups per reference. It's not all thatexciting right now, because we'd managed to minimize the number of placesthat need to fetch the data; but the upcoming writable-foreign-tables patchneeds this info in a lot more places.
1 parent9795113 commit1908abc

File tree

8 files changed

+72
-6
lines changed

8 files changed

+72
-6
lines changed

‎src/backend/commands/analyze.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ analyze_rel(Oid relid, VacuumStmt *vacstmt, BufferAccessStrategy bstrategy)
227227
FdwRoutine*fdwroutine;
228228
boolok= false;
229229

230-
fdwroutine=GetFdwRoutineByRelId(RelationGetRelid(onerel));
230+
fdwroutine=GetFdwRoutineForRelation(onerel, false);
231231

232232
if (fdwroutine->AnalyzeForeignTable!=NULL)
233233
ok=fdwroutine->AnalyzeForeignTable(onerel,

‎src/backend/executor/nodeForeignscan.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ ExecInitForeignScan(ForeignScan *node, EState *estate, int eflags)
160160
/*
161161
* Acquire function pointers from the FDW's handler, and init fdw_state.
162162
*/
163-
fdwroutine=GetFdwRoutineByRelId(RelationGetRelid(currentRelation));
163+
fdwroutine=GetFdwRoutineForRelation(currentRelation, true);
164164
scanstate->fdwroutine=fdwroutine;
165165
scanstate->fdw_state=NULL;
166166

‎src/backend/foreign/foreign.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include"lib/stringinfo.h"
2424
#include"miscadmin.h"
2525
#include"utils/builtins.h"
26+
#include"utils/memutils.h"
27+
#include"utils/rel.h"
2628
#include"utils/syscache.h"
2729

2830

@@ -352,6 +354,50 @@ GetFdwRoutineByRelId(Oid relid)
352354
returnGetFdwRoutine(fdwhandler);
353355
}
354356

357+
/*
358+
* GetFdwRoutineForRelation - look up the handler of the foreign-data wrapper
359+
* for the given foreign table, and retrieve its FdwRoutine struct.
360+
*
361+
* This function is preferred over GetFdwRoutineByRelId because it caches
362+
* the data in the relcache entry, saving a number of catalog lookups.
363+
*
364+
* If makecopy is true then the returned data is freshly palloc'd in the
365+
* caller's memory context. Otherwise, it's a pointer to the relcache data,
366+
* which will be lost in any relcache reset --- so don't rely on it long.
367+
*/
368+
FdwRoutine*
369+
GetFdwRoutineForRelation(Relationrelation,boolmakecopy)
370+
{
371+
FdwRoutine*fdwroutine;
372+
FdwRoutine*cfdwroutine;
373+
374+
if (relation->rd_fdwroutine==NULL)
375+
{
376+
/* Get the info by consulting the catalogs and the FDW code */
377+
fdwroutine=GetFdwRoutineByRelId(RelationGetRelid(relation));
378+
379+
/* Save the data for later reuse in CacheMemoryContext */
380+
cfdwroutine= (FdwRoutine*)MemoryContextAlloc(CacheMemoryContext,
381+
sizeof(FdwRoutine));
382+
memcpy(cfdwroutine,fdwroutine,sizeof(FdwRoutine));
383+
relation->rd_fdwroutine=cfdwroutine;
384+
385+
/* Give back the locally palloc'd copy regardless of makecopy */
386+
returnfdwroutine;
387+
}
388+
389+
/* We have valid cached data --- does the caller want a copy? */
390+
if (makecopy)
391+
{
392+
fdwroutine= (FdwRoutine*)palloc(sizeof(FdwRoutine));
393+
memcpy(fdwroutine,relation->rd_fdwroutine,sizeof(FdwRoutine));
394+
returnfdwroutine;
395+
}
396+
397+
/* Only a short-lived reference is needed, so just hand back cached copy */
398+
returnrelation->rd_fdwroutine;
399+
}
400+
355401

356402
/*
357403
* deflist_to_tuplestore - Helper function to convert DefElem list to

‎src/backend/optimizer/path/allpaths.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,6 @@ set_foreign_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
410410
/* Mark rel with estimated output rows, width, etc */
411411
set_foreign_size_estimates(root,rel);
412412

413-
/* Get FDW routine pointers for the rel */
414-
rel->fdwroutine=GetFdwRoutineByRelId(rte->relid);
415-
416413
/* Let FDW adjust the size estimates, if it can */
417414
rel->fdwroutine->GetForeignRelSize(root,rel,rte->relid);
418415
}

‎src/backend/optimizer/util/plancat.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include"access/xlog.h"
2727
#include"catalog/catalog.h"
2828
#include"catalog/heap.h"
29+
#include"foreign/fdwapi.h"
2930
#include"miscadmin.h"
3031
#include"nodes/makefuncs.h"
3132
#include"optimizer/clauses.h"
@@ -67,6 +68,7 @@ static List *build_index_tlist(PlannerInfo *root, IndexOptInfo *index,
6768
*min_attrlowest valid AttrNumber
6869
*max_attrhighest valid AttrNumber
6970
*indexlistlist of IndexOptInfos for relation's indexes
71+
*fdwroutineif it's a foreign table, the FDW function pointers
7072
*pagesnumber of pages
7173
*tuplesnumber of tuples
7274
*
@@ -374,6 +376,12 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
374376

375377
rel->indexlist=indexinfos;
376378

379+
/* Grab the fdwroutine info using the relcache, while we have it */
380+
if (relation->rd_rel->relkind==RELKIND_FOREIGN_TABLE)
381+
rel->fdwroutine=GetFdwRoutineForRelation(relation, true);
382+
else
383+
rel->fdwroutine=NULL;
384+
377385
heap_close(relation,NoLock);
378386

379387
/*

‎src/backend/utils/cache/relcache.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1846,6 +1846,8 @@ RelationDestroyRelation(Relation relation)
18461846
MemoryContextDelete(relation->rd_indexcxt);
18471847
if (relation->rd_rulescxt)
18481848
MemoryContextDelete(relation->rd_rulescxt);
1849+
if (relation->rd_fdwroutine)
1850+
pfree(relation->rd_fdwroutine);
18491851
pfree(relation);
18501852
}
18511853

@@ -4410,7 +4412,7 @@ load_relcache_init_file(bool shared)
44104412
* format is complex and subject to change). They must be rebuilt if
44114413
* needed by RelationCacheInitializePhase3. This is not expected to
44124414
* be a big performance hit since few system catalogs have such. Ditto
4413-
* for index expressions, predicates, andexclusion info.
4415+
* for index expressions, predicates,exclusion info,andFDW info.
44144416
*/
44154417
rel->rd_rules=NULL;
44164418
rel->rd_rulescxt=NULL;
@@ -4420,6 +4422,7 @@ load_relcache_init_file(bool shared)
44204422
rel->rd_exclops=NULL;
44214423
rel->rd_exclprocs=NULL;
44224424
rel->rd_exclstrats=NULL;
4425+
rel->rd_fdwroutine=NULL;
44234426

44244427
/*
44254428
* Reset transient-state fields in the relcache entry

‎src/include/foreign/fdwapi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,6 @@ typedef struct FdwRoutine
9696
/* Functions in foreign/foreign.c */
9797
externFdwRoutine*GetFdwRoutine(Oidfdwhandler);
9898
externFdwRoutine*GetFdwRoutineByRelId(Oidrelid);
99+
externFdwRoutine*GetFdwRoutineForRelation(Relationrelation,boolmakecopy);
99100

100101
#endif/* FDWAPI_H */

‎src/include/utils/rel.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,17 @@ typedef struct RelationData
165165
void*rd_amcache;/* available for use by index AM */
166166
Oid*rd_indcollation;/* OIDs of index collations */
167167

168+
/*
169+
* foreign-table support
170+
*
171+
* rd_fdwroutine must point to a single memory chunk palloc'd in
172+
* CacheMemoryContext. It will be freed and reset to NULL on a relcache
173+
* reset.
174+
*/
175+
176+
/* use "struct" here to avoid needing to include fdwapi.h: */
177+
structFdwRoutine*rd_fdwroutine;/* cached function pointers, or NULL */
178+
168179
/*
169180
* Hack for CLUSTER, rewriting ALTER TABLE, etc: when writing a new
170181
* version of a table, we need to make any toast pointers inserted into it

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp