forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commit1d2fe56
committed
Fix PL/Python for recursion and interleaved set-returning functions.
PL/Python failed if a PL/Python function was invoked recursively via SPI,since arguments are passed to the function in its global dictionary(a horrible decision that's far too ancient to undo) and it would deletethose dictionary entries on function exit, leaving the outer recursionlevel(s) without any arguments. Not deleting them would be little better,since the outer levels would then see the innermost level's arguments.Since PL/Python uses ValuePerCall mode for evaluating set-returningfunctions, it's possible for multiple executions of the same SRF to beinterleaved within a query. PL/Python failed in such a case, becauseit stored only one iterator per function, directly in the function'sPLyProcedure struct. Moreover, one interleaved instance of the SRFwould see argument values that should belong to another.Hence, invent code for saving and restoring the argument entries. To fixthe recursion case, we only need to save at recursive entry and restoreat recursive exit, so the overhead in non-recursive cases is negligible.To fix the SRF case, we have to save when suspending a SRF and restorewhen resuming it, which is potentially not negligible; but fortunatelythis is mostly a matter of manipulating Python object refcounts andshould not involve much physical data copying.Also, store the Python iterator and saved argument values in a structureassociated with the SRF call site rather than the function itself. Thisrequires adding a memory context deletion callback to ensure that the SRFstate is cleaned up if the calling query exits before running the SRF tocompletion. Without that we'd leak a refcount to the iterator object insuch a case, resulting in session-lifespan memory leakage. (In thepre-existing code, there was no memory leak because there was only oneiterator pointer, but what would happen is that the previous iteratorwould be resumed by the next query attempting to use the SRF. Hardly thesemantics we want.)We can buy back some of whatever overhead we've added by getting rid ofPLy_function_delete_args(), which seems a useless activity: there is noneed to delete argument entries from the global dictionary on exit,since the next time anyone would see the global dict is on the nextfresh call of the PL/Python function, at which time we'd overwrite thoseentries with new arg values anyway.Also clean up some really ugly coding in the SRF implementation, includingsuch gems as returning directly out of a PG_TRY block. (The only reasonthat failed to crash hard was that all existing call sites immediatelyexited their own PG_TRY blocks, popping the dangling longjmp pointer beforethere was any chance of it being used.)In principle this is a bug fix; but it seems a bit too invasive relative toits value for a back-patch, and besides the fix depends on memory contextcallbacks so it could not go back further than 9.5 anyway.Alexey Grishchenko and Tom Lane1 parent11c8669 commit1d2fe56
File tree
7 files changed
+401
-73
lines changed- src/pl/plpython
- expected
- sql
7 files changed
+401
-73
lines changedLines changed: 29 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
124 | 124 |
| |
125 | 125 |
| |
126 | 126 |
| |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
127 | 156 |
| |
128 | 157 |
| |
129 | 158 |
| |
|
Lines changed: 15 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
57 | 57 |
| |
58 | 58 |
| |
59 | 59 |
| |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
60 | 69 |
| |
61 | 70 |
| |
62 | 71 |
| |
| |||
112 | 121 |
| |
113 | 122 |
| |
114 | 123 |
| |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
115 | 130 |
| |
116 | 131 |
| |
117 | 132 |
| |
|
0 commit comments
Comments
(0)