/*------------------------------------------------------------------------- * * vacuumlo.c * This removes orphaned large objects from a database. * * Copyright (c) 1994, Regents of the University of California * * * IDENTIFICATION * $Header: /cvsroot/pgsql/contrib/vacuumlo/vacuumlo.c,v 1.1 1999/04/10 16:48:05 peter Exp $ * *------------------------------------------------------------------------- */ #include #include #include #include #include #include #include #include "libpq-fe.h" #include "libpq/libpq-fs.h" #define BUFSIZE 1024 int vacuumlo(char *,int); /* * This vacuums a database. It returns 1 on success, -1 on failure. */ int vacuumlo(char *database,int verbose) { PGconn *conn; PGresult *res, *res2; char buf[BUFSIZE]; int matched=0; /* Number matched per scan */ int i; conn = PQsetdb(NULL, NULL, NULL, NULL, database); /* check to see that the backend connection was successfully made */ if (PQstatus(conn) == CONNECTION_BAD) { fprintf(stderr, "Connection to database '%s' failed.\n", database); fprintf(stderr, "%s", PQerrorMessage(conn)); return -1; } if(verbose) fprintf(stdout,"Connected to %s\n",database); /* * First we create and populate the lo temp table */ buf[0]='\0'; strcat(buf,"SELECT oid AS lo "); strcat(buf,"INTO TEMP TABLE vacuum_l "); strcat(buf,"FROM pg_class "); strcat(buf,"WHERE relkind='l'"); if(!(res = PQexec(conn,buf))) { fprintf(stderr,"Failed to create temp table.\n"); PQfinish(conn); return -1; } PQclear(res); /* * Now find any candidate tables who have columns of type oid (the column * oid is ignored, as it has attnum < 1) */ buf[0]='\0'; strcat(buf,"SELECT c.relname, a.attname "); strcat(buf,"FROM pg_class c, pg_attribute a, pg_type t "); strcat(buf,"WHERE a.attnum > 0 "); strcat(buf," AND a.attrelid = c.oid "); strcat(buf," AND a.atttypid = t.oid "); strcat(buf," AND t.typname = 'oid' "); strcat(buf," AND c.relname NOT LIKE 'pg_%'"); if(!(res = PQexec(conn,buf))) { fprintf(stderr,"Failed to create temp table.\n"); PQfinish(conn); return -1; } for(i=0;i