Re: Why plpython functions increase transaction counter much more then plpgsql functions?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Michał Albrycht <michalalbrycht(at)gmail(dot)com>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Why plpython functions increase transaction counter much more then plpgsql functions?
Date: 2024-11-08 14:39:48
Message-ID: 3628359.1731076788@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

=?UTF-8?Q?Micha=C5=82_Albrycht?= <michalalbrycht(at)gmail(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Michał Albrycht 2024-11-08 17:44:40 Re: Why plpython functions increase transaction counter much more then plpgsql functions?
Previous Message Ron Johnson 2024-11-08 14:03:30 Re: Why plpython functions increase transaction counter much more then plpgsql functions?