From: Rene P. <re...@la...> - 2003-11-29 16:37:22
|
Hello, When I try to issue the statement "vacuum analyze" I get an exception with the error message "VACUUM cannot run inside a BEGIN/END block". This is the code that reproduces the problem: from pyPgSQL import PgSQL db =3D PgSQL.connect(host=3D"myhost", database=3D"mydb", user=3D"myuser", password=3D"mypw") cursor =3D db.cursor() cursor.execute("vacuum analyze") I use pyPgSQL version 2.4 with Python 2.2 on Windows XP. The server is PostgreSQL 7.2.1 on Linux. Is this considered a bug in pyPgSQL? Is there any way to issue "vacuum analyze" from within a Python program? The reason I'm doing this is that my program inserts and deletes lots of records. And the PostgreSQL manual says: "After adding or deleting a large number of records, it may be a good idea to issue a VACUUM ANALYZE command for the affected table. This will update the system catalogs with the results of all recent changes, and allow the PostgreSQL query optimizer to make better choices in planning user queries." Thanks in advance for your help. --=20 Regards / Groeten, http://www.leren.nl Ren=E9 Pijlman http://www.applinet.nl =20 |
From: Karsten H. <Kar...@gm...> - 2003-11-29 17:16:12
|
> cursor.execute("vacuum analyze") cursor.execute('end;vacuum analyze;begin;') we use this successfully around "create database" -- GPG key ID E4071346 @ wwwkeys.pgp.net E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346 |
From: Rene P. <re...@la...> - 2003-11-29 18:12:06
|
Karsten Hilbert: >> cursor.execute("vacuum analyze") >cursor.execute('end;vacuum analyze;begin;') Thanks Karsten. It looks like a bug to me, but this is a good workaround. I'm too new to pyPgSQL to suggest a patch. Maybe later. --=20 Regards / Groeten, http://www.leren.nl Ren=E9 Pijlman http://www.applinet.nl =20 |
From: Karsten H. <Kar...@gm...> - 2003-11-29 18:36:09
|
> Thanks Karsten. It looks like a bug to me I don't think it's a bug but we'd have to check the PG docs to see what's the rationale behind vacuum analyze having to be outside a transaction. Karsten -- GPG key ID E4071346 @ wwwkeys.pgp.net E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346 |
From: Adam B. <ad...@no...> - 2003-11-30 14:09:05
|
On Sat, Nov 29, 2003 at 06:13:35PM +0100, Karsten Hilbert wrote: > cursor.execute('end;vacuum analyze;begin;') > we use this successfully around "create database" Don't you have a feeling it's a bit ugly? ;-) DBI, and especially cursor objects, weren't invented for DDL commands, only for DMLs. Personally, I think that DBI spec should be somehow enhanced so that it would cover such situations. As an acceptable solution I usually use plain libpq module here. The goal can be achieved with following code: from pyPgSQL import PgSQL conn = PgSQL.connect(...) ... conn.conn.query("vacuum analyze") Of course it will work with pyPgSQL only :^) Best regards, -- Adam Buraczewski <adamb (at) nor (dot) pl> * Linux user #165585 GCS/TW d- s-:+>+:- a C+++(++++) UL++++$ P++ L++++ E++ W+ N++ o? K w-- O M- V- PS+ !PE Y PGP+ t+ 5 X+ R tv- b+ DI D G++ e+++>++++ h r+>++ y? |
From: Billy G. A. <bil...@mu...> - 2003-12-02 06:16:19
|
Adam Buraczewski wrote: >On Sat, Nov 29, 2003 at 06:13:35PM +0100, Karsten Hilbert wrote: > > >>cursor.execute('end;vacuum analyze;begin;') >>we use this successfully around "create database" >> >> > >Don't you have a feeling it's a bit ugly? ;-) DBI, and especially >cursor objects, weren't invented for DDL commands, only for DMLs. >Personally, I think that DBI spec should be somehow enhanced so that >it would cover such situations. As an acceptable solution I usually >use plain libpq module here. The goal can be achieved with following >code: > > from pyPgSQL import PgSQL > conn = PgSQL.connect(...) > ... > conn.conn.query("vacuum analyze") > >Of course it will work with pyPgSQL only :^) > >Best regards, > > > Actually, you can just set autocommit to on (per the DB-API spec it's off by default). If autocommit is on, then pyPgSQL won't wrap the query within a begin ... end block. from pyPgSQL import PgSQL conn - PgSQL.connect(...) conn.autocommit = 1 curs = conn.cursor() curs.execute("vacuum analyze") *Note: *You must set autocommit to 0 /before/ creating any cursors. Of course, using the libpq.conn.query method as suggested by Adam avoids the need to create a cursor object, but you still need to be sure that there are no open cursors for the connection when you use (opening a connection with autocommit == 0 will open a transaction at cursor creations time). |
From: Rene P. <re...@la...> - 2003-12-02 11:10:53
|
Billy G. Allie: >Of course, using the libpq.conn.query method as suggested by Adam avoids= =20 >the need to create a cursor object, but you still need to be sure that=20 >there are no open cursors for the connection when you use (opening a=20 >connection with autocommit =3D=3D 0 will open a transaction at cursor=20 >creations time). I see. I'm doing vacuum halfway through my algorithm (after batch inserts/updates, before selects) and I'm beginning to think its best to do the vacuum on a separate connection with autocommit enabled. --=20 Regards / Groeten, http://www.leren.nl Ren=E9 Pijlman http://www.applinet.nl =20 |