|
23 | 23 | #include"lib/stringinfo.h"
|
24 | 24 | #include"miscadmin.h"
|
25 | 25 | #include"utils/builtins.h"
|
| 26 | +#include"utils/memutils.h" |
| 27 | +#include"utils/rel.h" |
26 | 28 | #include"utils/syscache.h"
|
27 | 29 |
|
28 | 30 |
|
@@ -352,6 +354,50 @@ GetFdwRoutineByRelId(Oid relid)
|
352 | 354 | returnGetFdwRoutine(fdwhandler);
|
353 | 355 | }
|
354 | 356 |
|
| 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 | + |
355 | 401 |
|
356 | 402 | /*
|
357 | 403 | * deflist_to_tuplestore - Helper function to convert DefElem list to
|
|