Anonymous 1541
> WARNING: there is already a transaction in progress
Ever since I started using transactions, I keep getting this "warning" (I consider it an error), even if all I do is literally:
pg_query('BEGIN');
It still logs that warning. A transaction is already in progress, somehow.
I know that every individual query is a transaction by default, but I don't understand why that matters when I literally run the query "BEGIN". Is it actually executing:
BEGIN;
BEGIN;
COMMIT;
? If so, that's obviously not what I intended. Am I supposed to set some kind of configuration option to "manually handle transactions"? I couldn't find anything related to that in `postgresql.conf`.
Context: PHP, pg_* functions.
Top Answer
Jack Douglas
I do not get any warning logged, in either the PHP error log or the postgres error log, when all I run is `pg_query('BEGIN');`
However I do get a single warning in the [postgres error log](https://www.postgresql.org/docs/12/runtime-config-logging.html) when I run it twice:
```
$result = pg_query('BEGIN');
$result = pg_query('BEGIN');
```
``` none
2020-09-04 11:32:28 BST WARNING: there is already a transaction in progress
```
This is consistent with the [documented behaviour](https://www.postgresql.org/docs/12/sql-begin.html):
> Issuing `BEGIN` when already inside a transaction block will provoke a warning message. The state of the transaction is not affected. To nest transactions within a transaction block, use savepoints (see `SAVEPOINT`).
As an aside, it's worth knowing that `pg_query` [is a bit quirky](https://www.php.net/manual/en/function.pg-query.php) regarding transactions:
> …When multiple statements are passed to the function, they are automatically executed as one transaction, unless there are explicit `BEGIN`/`COMMIT` commands included in the query string…
Answer #2
David
See also [OP's comment][c]—reproduced here for convenience—which describes the root of the problem:
> I found out what the issue was, and it was due to my “complex” system after all. You see, for every query, I make a “log query”, which itself uses a transaction block. That’s why that was logged all the time and I couldn’t reproduce it with “raw” `pg_*` code. So, the core issue has been found, and now figuring out how to avoid that is up to me and cannot be explained by anyone outside.
[c]: https://topanswers.xyz/transcript?room=1334&id=67079#c67079