=?UTF-8?Q?Micha=C5=82_Albrycht?= <michalalbrycht@gmail.com> writes:
> This proves that the plpython function affects the transaction counter much
> more. Does anyone know why?
A quick look at PLy_spi_execute_query shows that it runs each command
in a subtransaction. It pretty much has to, because the coding rules
for a Python method don't permit it to just longjmp out of the Python
interpreter, so it has to set up a subtransaction so it can catch any
error. In this example, each subtransaction will consume an XID.
The plpgsql example is different because it doesn't trap errors,
hence no subtransaction needed, and all the rows will get inserted
under the XID of the outer command's main transaction. If you were
to wrap the insert_row_to_db call in BEGIN ... EXCEPTION then it'd
consume the same number of XIDs as plpython, for the same reason.
> Is there anything I can do about it?
Batch the DB updates, perhaps?
> What's interesting it happens only if the function called by plpyhon makes
> changes to DB.
Totally unsurprising. XIDs are acquired only when the current
transaction or subtransaction first needs to change the DB.
regards, tom lane