23
23
24
24
#include "catalog/pg_class_d.h"
25
25
26
+ #include "common/logging.h"
26
27
#include "fe_utils/connect.h"
27
28
#include "libpq-fe.h"
28
29
#include "pg_getopt.h"
@@ -109,8 +110,7 @@ vacuumlo(const char *database, const struct _param *param)
109
110
conn = PQconnectdbParams (keywords , values , true);
110
111
if (!conn )
111
112
{
112
- fprintf (stderr , "Connection to database \"%s\" failed\n" ,
113
- database );
113
+ pg_log_error ("connection to database \"%s\" failed" , database );
114
114
return -1 ;
115
115
}
116
116
@@ -129,8 +129,8 @@ vacuumlo(const char *database, const struct _param *param)
129
129
/* check to see that the backend connection was successfully made */
130
130
if (PQstatus (conn ) == CONNECTION_BAD )
131
131
{
132
- fprintf ( stderr , "Connection to database \"%s\" failed:\n %s" ,
133
- database , PQerrorMessage (conn ));
132
+ pg_log_error ( "connection to database \"%s\" failed: %s" ,
133
+ database , PQerrorMessage (conn ));
134
134
PQfinish (conn );
135
135
return -1 ;
136
136
}
@@ -145,8 +145,7 @@ vacuumlo(const char *database, const struct _param *param)
145
145
res = PQexec (conn , ALWAYS_SECURE_SEARCH_PATH_SQL );
146
146
if (PQresultStatus (res ) != PGRES_TUPLES_OK )
147
147
{
148
- fprintf (stderr , "Failed to set search_path:\n" );
149
- fprintf (stderr , "%s" , PQerrorMessage (conn ));
148
+ pg_log_error ("failed to set search_path: %s" , PQerrorMessage (conn ));
150
149
PQclear (res );
151
150
PQfinish (conn );
152
151
return -1 ;
@@ -165,8 +164,7 @@ vacuumlo(const char *database, const struct _param *param)
165
164
res = PQexec (conn , buf );
166
165
if (PQresultStatus (res ) != PGRES_COMMAND_OK )
167
166
{
168
- fprintf (stderr , "Failed to create temp table:\n" );
169
- fprintf (stderr , "%s" , PQerrorMessage (conn ));
167
+ pg_log_error ("failed to create temp table: %s" , PQerrorMessage (conn ));
170
168
PQclear (res );
171
169
PQfinish (conn );
172
170
return -1 ;
@@ -182,8 +180,7 @@ vacuumlo(const char *database, const struct _param *param)
182
180
res = PQexec (conn , buf );
183
181
if (PQresultStatus (res ) != PGRES_COMMAND_OK )
184
182
{
185
- fprintf (stderr , "Failed to vacuum temp table:\n" );
186
- fprintf (stderr , "%s" , PQerrorMessage (conn ));
183
+ pg_log_error ("failed to vacuum temp table: %s" , PQerrorMessage (conn ));
187
184
PQclear (res );
188
185
PQfinish (conn );
189
186
return -1 ;
@@ -212,8 +209,7 @@ vacuumlo(const char *database, const struct _param *param)
212
209
res = PQexec (conn , buf );
213
210
if (PQresultStatus (res ) != PGRES_TUPLES_OK )
214
211
{
215
- fprintf (stderr , "Failed to find OID columns:\n" );
216
- fprintf (stderr , "%s" , PQerrorMessage (conn ));
212
+ pg_log_error ("failed to find OID columns: %s" , PQerrorMessage (conn ));
217
213
PQclear (res );
218
214
PQfinish (conn );
219
215
return -1 ;
@@ -238,7 +234,7 @@ vacuumlo(const char *database, const struct _param *param)
238
234
239
235
if (!schema || !table || !field )
240
236
{
241
- fprintf ( stderr , "%s" , PQerrorMessage (conn ));
237
+ pg_log_error ( "%s" , PQerrorMessage (conn ));
242
238
PQclear (res );
243
239
PQfinish (conn );
244
240
if (schema != NULL )
@@ -257,9 +253,8 @@ vacuumlo(const char *database, const struct _param *param)
257
253
res2 = PQexec (conn , buf );
258
254
if (PQresultStatus (res2 ) != PGRES_COMMAND_OK )
259
255
{
260
- fprintf (stderr , "Failed to check %s in table %s.%s:\n" ,
261
- field , schema , table );
262
- fprintf (stderr , "%s" , PQerrorMessage (conn ));
256
+ pg_log_error ("failed to check %s in table %s.%s: %s" ,
257
+ field , schema , table , PQerrorMessage (conn ));
263
258
PQclear (res2 );
264
259
PQclear (res );
265
260
PQfinish (conn );
@@ -288,8 +283,7 @@ vacuumlo(const char *database, const struct _param *param)
288
283
res = PQexec (conn , "begin" );
289
284
if (PQresultStatus (res ) != PGRES_COMMAND_OK )
290
285
{
291
- fprintf (stderr , "Failed to start transaction:\n" );
292
- fprintf (stderr , "%s" , PQerrorMessage (conn ));
286
+ pg_log_error ("failed to start transaction: %s" , PQerrorMessage (conn ));
293
287
PQclear (res );
294
288
PQfinish (conn );
295
289
return -1 ;
@@ -302,7 +296,7 @@ vacuumlo(const char *database, const struct _param *param)
302
296
res = PQexec (conn , buf );
303
297
if (PQresultStatus (res ) != PGRES_COMMAND_OK )
304
298
{
305
- fprintf ( stderr , "DECLARE CURSOR failed: %s" , PQerrorMessage (conn ));
299
+ pg_log_error ( "DECLARE CURSOR failed: %s" , PQerrorMessage (conn ));
306
300
PQclear (res );
307
301
PQfinish (conn );
308
302
return -1 ;
@@ -319,7 +313,7 @@ vacuumlo(const char *database, const struct _param *param)
319
313
res = PQexec (conn , buf );
320
314
if (PQresultStatus (res ) != PGRES_TUPLES_OK )
321
315
{
322
- fprintf ( stderr , "FETCH FORWARD failed: %s" , PQerrorMessage (conn ));
316
+ pg_log_error ( "FETCH FORWARD failed: %s" , PQerrorMessage (conn ));
323
317
PQclear (res );
324
318
PQfinish (conn );
325
319
return -1 ;
@@ -347,8 +341,8 @@ vacuumlo(const char *database, const struct _param *param)
347
341
{
348
342
if (lo_unlink (conn , lo ) < 0 )
349
343
{
350
- fprintf ( stderr , "\nFailed to remove lo %u: " , lo );
351
- fprintf ( stderr , "%s" , PQerrorMessage (conn ));
344
+ pg_log_error ( "failed to remove lo %u: %s " , lo ,
345
+ PQerrorMessage (conn ));
352
346
if (PQtransactionStatus (conn ) == PQTRANS_INERROR )
353
347
{
354
348
success = false;
@@ -367,8 +361,8 @@ vacuumlo(const char *database, const struct _param *param)
367
361
res2 = PQexec (conn , "commit" );
368
362
if (PQresultStatus (res2 ) != PGRES_COMMAND_OK )
369
363
{
370
- fprintf ( stderr , "Failed to commit transaction:\n" );
371
- fprintf ( stderr , "%s" , PQerrorMessage (conn ));
364
+ pg_log_error ( "failed to commit transaction: %s" ,
365
+ PQerrorMessage (conn ));
372
366
PQclear (res2 );
373
367
PQclear (res );
374
368
PQfinish (conn );
@@ -378,8 +372,8 @@ vacuumlo(const char *database, const struct _param *param)
378
372
res2 = PQexec (conn , "begin" );
379
373
if (PQresultStatus (res2 ) != PGRES_COMMAND_OK )
380
374
{
381
- fprintf ( stderr , "Failed to start transaction:\n" );
382
- fprintf ( stderr , "%s" , PQerrorMessage (conn ));
375
+ pg_log_error ( "failed to start transaction: %s" ,
376
+ PQerrorMessage (conn ));
383
377
PQclear (res2 );
384
378
PQclear (res );
385
379
PQfinish (conn );
@@ -398,8 +392,8 @@ vacuumlo(const char *database, const struct _param *param)
398
392
res = PQexec (conn , "commit" );
399
393
if (PQresultStatus (res ) != PGRES_COMMAND_OK )
400
394
{
401
- fprintf ( stderr , "Failed to commit transaction:\n" );
402
- fprintf ( stderr , "%s" , PQerrorMessage (conn ));
395
+ pg_log_error ( "failed to commit transaction: %s" ,
396
+ PQerrorMessage (conn ));
403
397
PQclear (res );
404
398
PQfinish (conn );
405
399
return -1 ;
@@ -471,6 +465,7 @@ main(int argc, char **argv)
471
465
const char * progname ;
472
466
int optindex ;
473
467
468
+ pg_logging_init (argv [0 ]);
474
469
progname = get_progname (argv [0 ]);
475
470
476
471
/* Set default parameter values */
@@ -512,9 +507,7 @@ main(int argc, char **argv)
512
507
param .transaction_limit = strtol (optarg , NULL , 10 );
513
508
if (param .transaction_limit < 0 )
514
509
{
515
- fprintf (stderr ,
516
- "%s: transaction limit must not be negative (0 disables)\n" ,
517
- progname );
510
+ pg_log_error ("transaction limit must not be negative (0 disables)" );
518
511
exit (1 );
519
512
}
520
513
break ;
@@ -526,7 +519,7 @@ main(int argc, char **argv)
526
519
port = strtol (optarg , NULL , 10 );
527
520
if ((port < 1 ) || (port > 65535 ))
528
521
{
529
- fprintf ( stderr , "%s: invalid port number: %s\n" , progname , optarg );
522
+ pg_log_error ( " invalid port number: %s" , optarg );
530
523
exit (1 );
531
524
}
532
525
param .pg_port = pg_strdup (optarg );
@@ -552,7 +545,7 @@ main(int argc, char **argv)
552
545
/* No database given? Show usage */
553
546
if (optind >= argc )
554
547
{
555
- fprintf ( stderr , "vacuumlo: missing required argument: database name\n " );
548
+ pg_log_error ( " missing required argument: database name" );
556
549
fprintf (stderr , _ ("Try \"%s --help\" for more information.\n" ), progname );
557
550
exit (1 );
558
551
}
0 commit comments