57.5. Row Locking in Foreign Data Wrappers | ||||
---|---|---|---|---|
Prev | Up | Chapter 57. Writing a Foreign Data Wrapper | Home | Next |
57.5. Row Locking in Foreign Data Wrappers#
If an FDW's underlying storage mechanism has a concept of locking individual rows to prevent concurrent updates of those rows, it is usually worthwhile for the FDW to perform row-level locking with as close an approximation as practical to the semantics used in ordinaryPostgreSQL tables. There are multiple considerations involved in this.
One key decision to be made is whether to performearly locking orlate locking. In early locking, a row is locked when it is first retrieved from the underlying store, while in late locking, the row is locked only when it is known that it needs to be locked. (The difference arises because some rows may be discarded by locally-checked restriction or join conditions.) Early locking is much simpler and avoids extra round trips to a remote store, but it can cause locking of rows that need not have been locked, resulting in reduced concurrency or even unexpected deadlocks. Also, late locking is only possible if the row to be locked can be uniquely re-identified later. Preferably the row identifier should identify a specific version of the row, asPostgreSQL TIDs do.
By default,PostgreSQL ignores locking considerations when interfacing to FDWs, but an FDW can perform early locking without any explicit support from the core code. The API functions described inSection 57.2.6, which were added inPostgreSQL 9.5, allow an FDW to use late locking if it wishes.
An additional consideration is that inREAD COMMITTED
isolation mode,PostgreSQL may need to re-check restriction and join conditions against an updated version of some target tuple. Rechecking join conditions requires re-obtaining copies of the non-target rows that were previously joined to the target tuple. When working with standardPostgreSQL tables, this is done by including the TIDs of the non-target tables in the column list projected through the join, and then re-fetching non-target rows when required. This approach keeps the join data set compact, but it requires inexpensive re-fetch capability, as well as a TID that can uniquely identify the row version to be re-fetched. By default, therefore, the approach used with foreign tables is to include a copy of the entire row fetched from a foreign table in the column list projected through the join. This puts no special demands on the FDW but can result in reduced performance of merge and hash joins. An FDW that is capable of meeting the re-fetch requirements can choose to do it the first way.
For anUPDATE
orDELETE
on a foreign table, it is recommended that theForeignScan
operation on the target table perform early locking on the rows that it fetches, perhaps via the equivalent ofSELECT FOR UPDATE
. An FDW can detect whether a table is anUPDATE
/DELETE
target at plan time by comparing its relid toroot->parse->resultRelation
, or at execution time by usingExecRelationIsTargetRelation()
. An alternative possibility is to perform late locking within theExecForeignUpdate
orExecForeignDelete
callback, but no special support is provided for this.
For foreign tables that are specified to be locked by aSELECT FOR UPDATE/SHARE
command, theForeignScan
operation can again perform early locking by fetching tuples with the equivalent ofSELECT FOR UPDATE/SHARE
. To perform late locking instead, provide the callback functions defined inSection 57.2.6. InGetForeignRowMarkType
, select rowmark optionROW_MARK_EXCLUSIVE
,ROW_MARK_NOKEYEXCLUSIVE
,ROW_MARK_SHARE
, orROW_MARK_KEYSHARE
depending on the requested lock strength. (The core code will act the same regardless of which of these four options you choose.) Elsewhere, you can detect whether a foreign table was specified to be locked by this type of command by usingget_plan_rowmark
at plan time, orExecFindRowMark
at execution time; you must check not only whether a non-null rowmark struct is returned, but that itsstrength
field is notLCS_NONE
.
Lastly, for foreign tables that are used in anUPDATE
,DELETE
orSELECT FOR UPDATE/SHARE
command but are not specified to be row-locked, you can override the default choice to copy entire rows by havingGetForeignRowMarkType
select optionROW_MARK_REFERENCE
when it sees lock strengthLCS_NONE
. This will causeRefetchForeignRow
to be called with that value formarkType
; it should then re-fetch the row without acquiring any new lock. (If you have aGetForeignRowMarkType
function but don't wish to re-fetch unlocked rows, select optionROW_MARK_COPY
forLCS_NONE
.)
Seesrc/include/nodes/lockoptions.h
, the comments forRowMarkType
andPlanRowMark
insrc/include/nodes/plannodes.h
, and the comments forExecRowMark
insrc/include/nodes/execnodes.h
for additional information.