* GetFdwRoutineForRelation - look up the handler of the foreign-data wrapper * for the given foreign table, and retrieve its FdwRoutine struct. * * This function is preferred over GetFdwRoutineByRelId because it caches * the data in the relcache entry, saving a number of catalog lookups. * * If makecopy is true then the returned data is freshly palloc'd in the * caller's memory context. Ot
| 693 | * which will be lost in any relcache reset --- so don't rely on it long. |
| 694 | */ |
| 695 | FdwRoutine * |
| 696 | GetFdwRoutineForRelation(Relation relation, bool makecopy) |
| 697 | { |
| 698 | FdwRoutine *fdwroutine; |
| 699 | FdwRoutine *cfdwroutine; |
| 700 | |
| 701 | if (relation->rd_fdwroutine == NULL) |
| 702 | { |
| 703 | /* Get the info by consulting the catalogs and the FDW code */ |
| 704 | fdwroutine = GetFdwRoutineByRelId(RelationGetRelid(relation)); |
| 705 | |
| 706 | /* Save the data for later reuse in CacheMemoryContext */ |
| 707 | cfdwroutine = (FdwRoutine *) MemoryContextAlloc(CacheMemoryContext, |
| 708 | sizeof(FdwRoutine)); |
| 709 | memcpy(cfdwroutine, fdwroutine, sizeof(FdwRoutine)); |
| 710 | relation->rd_fdwroutine = cfdwroutine; |
| 711 | |
| 712 | /* Give back the locally palloc'd copy regardless of makecopy */ |
| 713 | return fdwroutine; |
| 714 | } |
| 715 | |
| 716 | /* We have valid cached data --- does the caller want a copy? */ |
| 717 | if (makecopy) |
| 718 | { |
| 719 | fdwroutine = (FdwRoutine *) palloc(sizeof(FdwRoutine)); |
| 720 | memcpy(fdwroutine, relation->rd_fdwroutine, sizeof(FdwRoutine)); |
| 721 | return fdwroutine; |
| 722 | } |
| 723 | |
| 724 | /* Only a short-lived reference is needed, so just hand back cached copy */ |
| 725 | return relation->rd_fdwroutine; |
| 726 | } |
| 727 | |
| 728 | |
| 729 | /* |
no test coverage detected