@@ -290,74 +290,101 @@ vacuumlo(const char *database, const struct _param * param)
290290PQclear (res );
291291
292292buf [0 ]= '\0' ;
293- strcat (buf ,"SELECT lo FROM vacuum_l" );
293+ strcat (buf ,
294+ "DECLARE myportal CURSOR WITH HOLD FOR SELECT lo FROM vacuum_l" );
294295res = PQexec (conn ,buf );
295- if (PQresultStatus (res )!= PGRES_TUPLES_OK )
296+ if (PQresultStatus (res )!= PGRES_COMMAND_OK )
296297{
297- fprintf (stderr ,"Failed to read temp table:\n" );
298- fprintf (stderr ,"%s" ,PQerrorMessage (conn ));
298+ fprintf (stderr ,"DECLARE CURSOR failed: %s" ,PQerrorMessage (conn ));
299299PQclear (res );
300300PQfinish (conn );
301301return -1 ;
302302}
303+ PQclear (res );
304+
305+ snprintf (buf ,BUFSIZE ,"FETCH FORWARD %ld IN myportal" ,
306+ param -> transaction_limit > 0 ?param -> transaction_limit :1000L );
303307
304- matched = PQntuples (res );
305308deleted = 0 ;
306- for (i = 0 ;i < matched ;i ++ )
309+
310+ while (1 )
307311{
308- Oid lo = atooid (PQgetvalue (res ,i ,0 ));
312+ res = PQexec (conn ,buf );
313+ if (PQresultStatus (res )!= PGRES_TUPLES_OK )
314+ {
315+ fprintf (stderr ,"FETCH FORWARD failed: %s" ,PQerrorMessage (conn ));
316+ PQclear (res );
317+ PQfinish (conn );
318+ return -1 ;
319+ }
309320
310- if (param -> verbose )
321+ matched = PQntuples (res );
322+ if (matched <=0 )
311323{
312- fprintf (stdout ,"\rRemoving lo %6u " ,lo );
313- fflush (stdout );
324+ /* at end of resultset */
325+ PQclear (res );
326+ break ;
314327}
315328
316- if ( param -> dry_run == 0 )
329+ for ( i = 0 ; i < matched ; i ++ )
317330{
318- if (lo_unlink (conn ,lo )< 0 )
331+ Oid lo = atooid (PQgetvalue (res ,i ,0 ));
332+
333+ if (param -> verbose )
334+ {
335+ fprintf (stdout ,"\rRemoving lo %6u " ,lo );
336+ fflush (stdout );
337+ }
338+
339+ if (param -> dry_run == 0 )
319340{
320- fprintf (stderr ,"\nFailed to remove lo %u: " ,lo );
321- fprintf (stderr ,"%s" ,PQerrorMessage (conn ));
322- if (PQtransactionStatus (conn )== PQTRANS_INERROR )
341+ if (lo_unlink (conn ,lo )< 0 )
323342{
324- success = false;
325- break ;
343+ fprintf (stderr ,"\nFailed to remove lo %u: " ,lo );
344+ fprintf (stderr ,"%s" ,PQerrorMessage (conn ));
345+ if (PQtransactionStatus (conn )== PQTRANS_INERROR )
346+ {
347+ success = false;
348+ PQclear (res );
349+ break ;
350+ }
326351}
352+ else
353+ deleted ++ ;
327354}
328355else
329356deleted ++ ;
330- }
331- else
332- deleted ++ ;
333- if (param -> transaction_limit > 0 &&
334- (deleted %param -> transaction_limit )== 0 )
335- {
336- res2 = PQexec (conn ,"commit" );
337- if (PQresultStatus (res2 )!= PGRES_COMMAND_OK )
357+
358+ if (param -> transaction_limit > 0 &&
359+ (deleted %param -> transaction_limit )== 0 )
338360{
339- fprintf (stderr ,"Failed to commit transaction:\n" );
340- fprintf (stderr ,"%s" ,PQerrorMessage (conn ));
361+ res2 = PQexec (conn ,"commit" );
362+ if (PQresultStatus (res2 )!= PGRES_COMMAND_OK )
363+ {
364+ fprintf (stderr ,"Failed to commit transaction:\n" );
365+ fprintf (stderr ,"%s" ,PQerrorMessage (conn ));
366+ PQclear (res2 );
367+ PQclear (res );
368+ PQfinish (conn );
369+ return -1 ;
370+ }
341371PQclear (res2 );
342- PQclear ( res );
343- PQfinish ( conn );
344- return -1 ;
345- }
346- PQclear ( res2 );
347- res2 = PQexec ( conn , "begin" );
348- if ( PQresultStatus ( res2 ) != PGRES_COMMAND_OK )
349- {
350- fprintf ( stderr , "Failed to start transaction:\n" ) ;
351- fprintf ( stderr , "%s" , PQerrorMessage ( conn ));
372+ res2 = PQexec ( conn , "begin" );
373+ if ( PQresultStatus ( res2 ) != PGRES_COMMAND_OK )
374+ {
375+ fprintf ( stderr , "Failed to start transaction:\n" );
376+ fprintf ( stderr , "%s" , PQerrorMessage ( conn ) );
377+ PQclear ( res2 );
378+ PQclear ( res );
379+ PQfinish ( conn );
380+ return -1 ;
381+ }
352382PQclear (res2 );
353- PQclear (res );
354- PQfinish (conn );
355- return -1 ;
356383}
357- PQclear (res2 );
358384}
385+
386+ PQclear (res );
359387}
360- PQclear (res );
361388
362389/*
363390 * That's all folks!