PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
common.c File Reference
#include "postgres_fe.h"
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <pwd.h>
#include <signal.h>
#include <unistd.h>
#include "command.h"
#include "common.h"
#include "common/logging.h"
#include "copy.h"
#include "crosstabview.h"
#include "fe_utils/cancel.h"
#include "fe_utils/mbprint.h"
#include "fe_utils/string_utils.h"
#include "portability/instr_time.h"
#include "settings.h"
Include dependency graph for common.c:

Go to the source code of this file.

Functions

static bool DescribeQuery (const char *query, double *elapsed_msec)
 
static int ExecQueryAndProcessResults (const char *query, double *elapsed_msec, bool *svpt_gone_p, bool is_watch, int min_rows, const printQueryOpt *opt, FILE *printQueryFout)
 
static bool command_no_begin (const char *query)
 
bool openQueryOutputFile (const char *fname, FILE **fout, bool *is_pipe)
 
static bool SetupGOutput (FILE **gfile_fout, bool *is_pipe)
 
static void CloseGOutput (FILE *gfile_fout, bool is_pipe)
 
static void pipelineReset (void)
 
bool setQFout (const char *fname)
 
char * psql_get_variable (const char *varname, PsqlScanQuoteType quote, void *passthrough)
 
void NoticeProcessor (void *arg, const char *message)
 
static void psql_cancel_callback (void)
 
void psql_setup_cancel_handler (void)
 
static bool ConnectionUp (void)
 
static bool CheckConnection (void)
 
static bool AcceptResult (const PGresult *result, bool show_error)
 
static void SetResultVariables (PGresult *result, bool success)
 
void SetShellResultVariables (int wait_result)
 
static void SetPipelineVariables (void)
 
static void ClearOrSaveResult (PGresult *result)
 
static void ClearOrSaveAllResults (void)
 
static void PrintTiming (double elapsed_msec)
 
PGresultPSQLexec (const char *query)
 
int PSQLexecWatch (const char *query, const printQueryOpt *opt, FILE *printQueryFout, int min_rows)
 
static void PrintNotifications (void)
 
static bool PrintQueryTuples (const PGresult *result, const printQueryOpt *opt, FILE *printQueryFout)
 
static bool StoreQueryTuple (const PGresult *result)
 
static bool ExecQueryTuples (const PGresult *result)
 
static bool HandleCopyResult (PGresult **resultp, FILE *copystream)
 
static void PrintQueryStatus (PGresult *result, FILE *printQueryFout)
 
static bool PrintQueryResult (PGresult *result, bool last, const printQueryOpt *opt, FILE *printQueryFout, FILE *printStatusFout)
 
bool SendQuery (const char *query)
 
static PGresultdiscardAbortedPipelineResults (void)
 
static const char * skip_white_space (const char *query)
 
bool is_superuser (void)
 
bool standard_strings (void)
 
const char * session_username (void)
 
char * get_conninfo_value (const char *keyword)
 
void expand_tilde (char **filename)
 
static int uri_prefix_length (const char *connstr)
 
void clean_extended_state (void)
 
bool recognized_connection_string (const char *connstr)
 

Variables

volatile sig_atomic_t sigint_interrupt_enabled = false
 
sigjmp_buf sigint_interrupt_jmp
 

Function Documentation

◆ AcceptResult()

static bool AcceptResult ( const PGresult result,
bool  show_error 
)
static

Definition at line 416 of file common.c.

417{
418 bool OK;
419
420 if (!result)
421 OK = false;
422 else
423 switch (PQresultStatus(result))
424 {
425 case PGRES_COMMAND_OK:
426 case PGRES_TUPLES_OK:
429 case PGRES_COPY_IN:
430 case PGRES_COPY_OUT:
432 /* Fine, do nothing */
433 OK = true;
434 break;
435
440 OK = false;
441 break;
442
443 default:
444 OK = false;
445 pg_log_error("unexpected PQresultStatus: %d",
446 PQresultStatus(result));
447 break;
448 }
449
450 if (!OK && show_error)
451 {
452 const char *error = PQerrorMessage(pset.db);
453
454 if (strlen(error))
455 pg_log_info("%s", error);
456
458 }
459
460 return OK;
461}
static bool CheckConnection(void)
Definition: common.c:354
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7679
#define PQresultStatus
Definition: libpq-be-fe.h:247
@ PGRES_COPY_IN
Definition: libpq-fe.h:132
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:125
@ PGRES_TUPLES_CHUNK
Definition: libpq-fe.h:142
@ PGRES_FATAL_ERROR
Definition: libpq-fe.h:136
@ PGRES_COPY_OUT
Definition: libpq-fe.h:131
@ PGRES_EMPTY_QUERY
Definition: libpq-fe.h:124
@ PGRES_PIPELINE_SYNC
Definition: libpq-fe.h:139
@ PGRES_BAD_RESPONSE
Definition: libpq-fe.h:133
@ PGRES_PIPELINE_ABORTED
Definition: libpq-fe.h:140
@ PGRES_NONFATAL_ERROR
Definition: libpq-fe.h:135
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:128
#define pg_log_error(...)
Definition: logging.h:106
#define pg_log_info(...)
Definition: logging.h:124
PsqlSettings pset
Definition: startup.c:32
static void error(void)
Definition: sql-dyntest.c:147
PGconn * db
Definition: settings.h:103

References CheckConnection(), _psqlSettings::db, error(), pg_log_error, pg_log_info, PGRES_BAD_RESPONSE, PGRES_COMMAND_OK, PGRES_COPY_IN, PGRES_COPY_OUT, PGRES_EMPTY_QUERY, PGRES_FATAL_ERROR, PGRES_NONFATAL_ERROR, PGRES_PIPELINE_ABORTED, PGRES_PIPELINE_SYNC, PGRES_TUPLES_CHUNK, PGRES_TUPLES_OK, PQerrorMessage(), PQresultStatus, and pset.

Referenced by DescribeQuery(), ExecQueryAndProcessResults(), and PSQLexec().

◆ CheckConnection()

static bool CheckConnection ( void  )
static

Definition at line 354 of file common.c.

355{
356 bool OK;
357
358 OK = ConnectionUp();
359 if (!OK)
360 {
362 {
363 pg_log_error("connection to server was lost");
364 exit(EXIT_BADCONN);
365 }
366
367 fprintf(stderr, _("The connection to the server was lost. Attempting reset: "));
368 PQreset(pset.db);
370 OK = ConnectionUp();
371 if (!OK)
372 {
373 fprintf(stderr, _("Failed.\n"));
374
375 /*
376 * Transition to having no connection; but stash away the failed
377 * connection so that we can still refer to its parameters in a
378 * later \connect attempt. Keep the state cleanup here in sync
379 * with do_connect().
380 */
381 if (pset.dead_conn)
384 pset.db = NULL;
387 }
388 else
389 {
390 fprintf(stderr, _("Succeeded.\n"));
391
392 /*
393 * Re-sync, just in case anything changed. Keep this in sync with
394 * do_connect().
395 */
397 connection_warnings(false); /* Must be after SyncVariables */
398 }
399 }
400
401 return OK;
402}
static void pipelineReset(void)
Definition: common.c:128
static bool ConnectionUp(void)
Definition: common.c:336
void ResetCancelConn(void)
Definition: cancel.c:107
void UnsyncVariables(void)
Definition: command.c:4624
void SyncVariables(void)
Definition: command.c:4569
void connection_warnings(bool in_startup)
Definition: command.c:4441
#define fprintf(file, fmt, msg)
Definition: cubescan.l:21
#define _(x)
Definition: elog.c:91
void PQreset(PGconn *conn)
Definition: fe-connect.c:5319
void PQfinish(PGconn *conn)
Definition: fe-connect.c:5305
#define EXIT_BADCONN
Definition: settings.h:200
PGconn * dead_conn
Definition: settings.h:158
bool cur_cmd_interactive
Definition: settings.h:140

References _, connection_warnings(), ConnectionUp(), _psqlSettings::cur_cmd_interactive, _psqlSettings::db, _psqlSettings::dead_conn, EXIT_BADCONN, fprintf, pg_log_error, pipelineReset(), PQfinish(), PQreset(), pset, ResetCancelConn(), SyncVariables(), and UnsyncVariables().

Referenced by AcceptResult(), and ExecQueryAndProcessResults().

◆ clean_extended_state()

void clean_extended_state ( void  )

Definition at line 2660 of file common.c.

2661{
2662 int i;
2663
2664 switch (pset.send_mode)
2665 {
2666 case PSQL_SEND_EXTENDED_CLOSE: /* \close_prepared */
2668 break;
2669 case PSQL_SEND_EXTENDED_PARSE: /* \parse */
2671 break;
2672 case PSQL_SEND_EXTENDED_QUERY_PARAMS: /* \bind */
2673 case PSQL_SEND_EXTENDED_QUERY_PREPARED: /* \bind_named */
2674 for (i = 0; i < pset.bind_nparams; i++)
2678 pset.bind_params = NULL;
2679 break;
2680 case PSQL_SEND_QUERY:
2681 case PSQL_SEND_START_PIPELINE_MODE: /* \startpipeline */
2682 case PSQL_SEND_END_PIPELINE_MODE: /* \endpipeline */
2683 case PSQL_SEND_PIPELINE_SYNC: /* \syncpipeline */
2684 case PSQL_SEND_FLUSH: /* \flush */
2685 case PSQL_SEND_GET_RESULTS: /* \getresults */
2686 case PSQL_SEND_FLUSH_REQUEST: /* \flushrequest */
2687 break;
2688 }
2689
2690 pset.stmtName = NULL;
2692}
#define free(a)
Definition: header.h:65
int i
Definition: isn.c:77
@ PSQL_SEND_PIPELINE_SYNC
Definition: settings.h:78
@ PSQL_SEND_FLUSH
Definition: settings.h:81
@ PSQL_SEND_FLUSH_REQUEST
Definition: settings.h:82
@ PSQL_SEND_START_PIPELINE_MODE
Definition: settings.h:79
@ PSQL_SEND_QUERY
Definition: settings.h:73
@ PSQL_SEND_EXTENDED_QUERY_PARAMS
Definition: settings.h:76
@ PSQL_SEND_EXTENDED_PARSE
Definition: settings.h:75
@ PSQL_SEND_END_PIPELINE_MODE
Definition: settings.h:80
@ PSQL_SEND_GET_RESULTS
Definition: settings.h:83
@ PSQL_SEND_EXTENDED_CLOSE
Definition: settings.h:74
@ PSQL_SEND_EXTENDED_QUERY_PREPARED
Definition: settings.h:77
char * stmtName
Definition: settings.h:124
char ** bind_params
Definition: settings.h:123
PSQL_SEND_MODE send_mode
Definition: settings.h:120
int bind_nparams
Definition: settings.h:122

References _psqlSettings::bind_nparams, _psqlSettings::bind_params, free, i, pset, PSQL_SEND_END_PIPELINE_MODE, PSQL_SEND_EXTENDED_CLOSE, PSQL_SEND_EXTENDED_PARSE, PSQL_SEND_EXTENDED_QUERY_PARAMS, PSQL_SEND_EXTENDED_QUERY_PREPARED, PSQL_SEND_FLUSH, PSQL_SEND_FLUSH_REQUEST, PSQL_SEND_GET_RESULTS, PSQL_SEND_PIPELINE_SYNC, PSQL_SEND_QUERY, PSQL_SEND_START_PIPELINE_MODE, _psqlSettings::send_mode, and _psqlSettings::stmtName.

Referenced by exec_command_bind(), exec_command_bind_named(), exec_command_close_prepared(), exec_command_g(), exec_command_gexec(), exec_command_gset(), exec_command_parse(), exec_command_sendpipeline(), exec_command_watch(), and SendQuery().

◆ ClearOrSaveAllResults()

static void ClearOrSaveAllResults ( void  )
static

Definition at line 582 of file common.c.

583{
584 PGresult *result;
585
586 while ((result = PQgetResult(pset.db)) != NULL)
587 ClearOrSaveResult(result);
588}
static void ClearOrSaveResult(PGresult *result)
Definition: common.c:558
#define PQgetResult
Definition: libpq-be-fe.h:246

References ClearOrSaveResult(), _psqlSettings::db, PQgetResult, and pset.

Referenced by ExecQueryAndProcessResults().

◆ ClearOrSaveResult()

static void ClearOrSaveResult ( PGresult result)
static

Definition at line 558 of file common.c.

559{
560 if (result)
561 {
562 switch (PQresultStatus(result))
563 {
567 pset.last_error_result = result;
568 break;
569
570 default:
571 PQclear(result);
572 break;
573 }
574 }
575}
#define PQclear
Definition: libpq-be-fe.h:245
PGresult * last_error_result
Definition: settings.h:110

References _psqlSettings::last_error_result, PGRES_FATAL_ERROR, PGRES_NONFATAL_ERROR, PQclear, PQresultStatus, and pset.

Referenced by ClearOrSaveAllResults(), DescribeQuery(), ExecQueryAndProcessResults(), PSQLexec(), and SendQuery().

◆ CloseGOutput()

static void CloseGOutput ( FILE *  gfile_fout,
bool  is_pipe 
)
static

Definition at line 110 of file common.c.

111{
112 if (gfile_fout)
113 {
114 if (is_pipe)
115 {
116 SetShellResultVariables(pclose(gfile_fout));
118 }
119 else
120 fclose(gfile_fout);
121 }
122}
void SetShellResultVariables(int wait_result)
Definition: common.c:516
void restore_sigpipe_trap(void)
Definition: print.c:3062

References restore_sigpipe_trap(), and SetShellResultVariables().

Referenced by ExecQueryAndProcessResults().

◆ command_no_begin()

static bool command_no_begin ( const char *  query)
static

Definition at line 2265 of file common.c.

2266{
2267 int wordlen;
2268
2269 /*
2270 * First we must advance over any whitespace and comments.
2271 */
2272 query = skip_white_space(query);
2273
2274 /*
2275 * Check word length (since "beginx" is not "begin").
2276 */
2277 wordlen = 0;
2278 while (isalpha((unsigned char) query[wordlen]))
2279 wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2280
2281 /*
2282 * Transaction control commands. These should include every keyword that
2283 * gives rise to a TransactionStmt in the backend grammar, except for the
2284 * savepoint-related commands.
2285 *
2286 * (We assume that START must be START TRANSACTION, since there is
2287 * presently no other "START foo" command.)
2288 */
2289 if (wordlen == 5 && pg_strncasecmp(query, "abort", 5) == 0)
2290 return true;
2291 if (wordlen == 5 && pg_strncasecmp(query, "begin", 5) == 0)
2292 return true;
2293 if (wordlen == 5 && pg_strncasecmp(query, "start", 5) == 0)
2294 return true;
2295 if (wordlen == 6 && pg_strncasecmp(query, "commit", 6) == 0)
2296 return true;
2297 if (wordlen == 3 && pg_strncasecmp(query, "end", 3) == 0)
2298 return true;
2299 if (wordlen == 8 && pg_strncasecmp(query, "rollback", 8) == 0)
2300 return true;
2301 if (wordlen == 7 && pg_strncasecmp(query, "prepare", 7) == 0)
2302 {
2303 /* PREPARE TRANSACTION is a TC command, PREPARE foo is not */
2304 query += wordlen;
2305
2306 query = skip_white_space(query);
2307
2308 wordlen = 0;
2309 while (isalpha((unsigned char) query[wordlen]))
2310 wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2311
2312 if (wordlen == 11 && pg_strncasecmp(query, "transaction", 11) == 0)
2313 return true;
2314 return false;
2315 }
2316
2317 /*
2318 * Commands not allowed within transactions. The statements checked for
2319 * here should be exactly those that call PreventInTransactionBlock() in
2320 * the backend.
2321 */
2322 if (wordlen == 6 && pg_strncasecmp(query, "vacuum", 6) == 0)
2323 return true;
2324 if (wordlen == 7 && pg_strncasecmp(query, "cluster", 7) == 0)
2325 {
2326 /* CLUSTER with any arguments is allowed in transactions */
2327 query += wordlen;
2328
2329 query = skip_white_space(query);
2330
2331 if (isalpha((unsigned char) query[0]))
2332 return false; /* has additional words */
2333 return true; /* it's CLUSTER without arguments */
2334 }
2335
2336 if (wordlen == 6 && pg_strncasecmp(query, "create", 6) == 0)
2337 {
2338 query += wordlen;
2339
2340 query = skip_white_space(query);
2341
2342 wordlen = 0;
2343 while (isalpha((unsigned char) query[wordlen]))
2344 wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2345
2346 if (wordlen == 8 && pg_strncasecmp(query, "database", 8) == 0)
2347 return true;
2348 if (wordlen == 10 && pg_strncasecmp(query, "tablespace", 10) == 0)
2349 return true;
2350
2351 /* CREATE [UNIQUE] INDEX CONCURRENTLY isn't allowed in xacts */
2352 if (wordlen == 6 && pg_strncasecmp(query, "unique", 6) == 0)
2353 {
2354 query += wordlen;
2355
2356 query = skip_white_space(query);
2357
2358 wordlen = 0;
2359 while (isalpha((unsigned char) query[wordlen]))
2360 wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2361 }
2362
2363 if (wordlen == 5 && pg_strncasecmp(query, "index", 5) == 0)
2364 {
2365 query += wordlen;
2366
2367 query = skip_white_space(query);
2368
2369 wordlen = 0;
2370 while (isalpha((unsigned char) query[wordlen]))
2371 wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2372
2373 if (wordlen == 12 && pg_strncasecmp(query, "concurrently", 12) == 0)
2374 return true;
2375 }
2376
2377 return false;
2378 }
2379
2380 if (wordlen == 5 && pg_strncasecmp(query, "alter", 5) == 0)
2381 {
2382 query += wordlen;
2383
2384 query = skip_white_space(query);
2385
2386 wordlen = 0;
2387 while (isalpha((unsigned char) query[wordlen]))
2388 wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2389
2390 /* ALTER SYSTEM isn't allowed in xacts */
2391 if (wordlen == 6 && pg_strncasecmp(query, "system", 6) == 0)
2392 return true;
2393
2394 return false;
2395 }
2396
2397 /*
2398 * Note: these tests will match DROP SYSTEM and REINDEX TABLESPACE, which
2399 * aren't really valid commands so we don't care much. The other four
2400 * possible matches are correct.
2401 */
2402 if ((wordlen == 4 && pg_strncasecmp(query, "drop", 4) == 0) ||
2403 (wordlen == 7 && pg_strncasecmp(query, "reindex", 7) == 0))
2404 {
2405 query += wordlen;
2406
2407 query = skip_white_space(query);
2408
2409 wordlen = 0;
2410 while (isalpha((unsigned char) query[wordlen]))
2411 wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2412
2413 if (wordlen == 8 && pg_strncasecmp(query, "database", 8) == 0)
2414 return true;
2415 if (wordlen == 6 && pg_strncasecmp(query, "system", 6) == 0)
2416 return true;
2417 if (wordlen == 10 && pg_strncasecmp(query, "tablespace", 10) == 0)
2418 return true;
2419 if (wordlen == 5 && (pg_strncasecmp(query, "index", 5) == 0 ||
2420 pg_strncasecmp(query, "table", 5) == 0))
2421 {
2422 query += wordlen;
2423 query = skip_white_space(query);
2424 wordlen = 0;
2425 while (isalpha((unsigned char) query[wordlen]))
2426 wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2427
2428 /*
2429 * REINDEX [ TABLE | INDEX ] CONCURRENTLY are not allowed in
2430 * xacts.
2431 */
2432 if (wordlen == 12 && pg_strncasecmp(query, "concurrently", 12) == 0)
2433 return true;
2434 }
2435
2436 /* DROP INDEX CONCURRENTLY isn't allowed in xacts */
2437 if (wordlen == 5 && pg_strncasecmp(query, "index", 5) == 0)
2438 {
2439 query += wordlen;
2440
2441 query = skip_white_space(query);
2442
2443 wordlen = 0;
2444 while (isalpha((unsigned char) query[wordlen]))
2445 wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2446
2447 if (wordlen == 12 && pg_strncasecmp(query, "concurrently", 12) == 0)
2448 return true;
2449
2450 return false;
2451 }
2452
2453 return false;
2454 }
2455
2456 /* DISCARD ALL isn't allowed in xacts, but other variants are allowed. */
2457 if (wordlen == 7 && pg_strncasecmp(query, "discard", 7) == 0)
2458 {
2459 query += wordlen;
2460
2461 query = skip_white_space(query);
2462
2463 wordlen = 0;
2464 while (isalpha((unsigned char) query[wordlen]))
2465 wordlen += PQmblenBounded(&query[wordlen], pset.encoding);
2466
2467 if (wordlen == 3 && pg_strncasecmp(query, "all", 3) == 0)
2468 return true;
2469 return false;
2470 }
2471
2472 return false;
2473}
static const char * skip_white_space(const char *query)
Definition: common.c:2201
int PQmblenBounded(const char *s, int encoding)
Definition: fe-misc.c:1266
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69

References _psqlSettings::encoding, pg_strncasecmp(), PQmblenBounded(), pset, and skip_white_space().

Referenced by SendQuery().

◆ ConnectionUp()

static bool ConnectionUp ( void  )
static

Definition at line 336 of file common.c.

337{
338 return PQstatus(pset.db) != CONNECTION_BAD;
339}
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:7616
@ CONNECTION_BAD
Definition: libpq-fe.h:85

References CONNECTION_BAD, _psqlSettings::db, PQstatus(), and pset.

Referenced by CheckConnection(), and SendQuery().

◆ DescribeQuery()

static bool DescribeQuery ( const char *  query,
double *  elapsed_msec 
)
static

Definition at line 1350 of file common.c.

1351{
1352 bool timing = pset.timing;
1353 PGresult *result;
1354 bool OK;
1356 after;
1357
1358 *elapsed_msec = 0;
1359
1360 if (timing)
1362 else
1364
1365 /*
1366 * To parse the query but not execute it, we prepare it, using the unnamed
1367 * prepared statement. This is invisible to psql users, since there's no
1368 * way to access the unnamed prepared statement from psql user space. The
1369 * next Parse or Query protocol message would overwrite the statement
1370 * anyway. (So there's no great need to clear it when done, which is a
1371 * good thing because libpq provides no easy way to do that.)
1372 */
1373 result = PQprepare(pset.db, "", query, 0, NULL);
1374 if (PQresultStatus(result) != PGRES_COMMAND_OK)
1375 {
1377 SetResultVariables(result, false);
1378 ClearOrSaveResult(result);
1379 return false;
1380 }
1381 PQclear(result);
1382
1383 result = PQdescribePrepared(pset.db, "");
1384 OK = AcceptResult(result, true) &&
1385 (PQresultStatus(result) == PGRES_COMMAND_OK);
1386 if (OK && result)
1387 {
1388 if (PQnfields(result) > 0)
1389 {
1391 int i;
1392
1394
1396 "SELECT name AS \"%s\", pg_catalog.format_type(tp, tpm) AS \"%s\"\n"
1397 "FROM (VALUES ",
1398 gettext_noop("Column"),
1399 gettext_noop("Type"));
1400
1401 for (i = 0; i < PQnfields(result); i++)
1402 {
1403 const char *name;
1404 char *escname;
1405
1406 if (i > 0)
1408
1409 name = PQfname(result, i);
1410 escname = PQescapeLiteral(pset.db, name, strlen(name));
1411
1412 if (escname == NULL)
1413 {
1415 PQclear(result);
1417 return false;
1418 }
1419
1420 appendPQExpBuffer(&buf, "(%s, '%u'::pg_catalog.oid, %d)",
1421 escname,
1422 PQftype(result, i),
1423 PQfmod(result, i));
1424
1425 PQfreemem(escname);
1426 }
1427
1428 appendPQExpBufferStr(&buf, ") s(name, tp, tpm)");
1429 PQclear(result);
1430
1431 result = PQexec(pset.db, buf.data);
1432 OK = AcceptResult(result, true);
1433
1434 if (timing)
1435 {
1438 *elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
1439 }
1440
1441 if (OK && result)
1442 OK = PrintQueryResult(result, true, NULL, NULL, NULL);
1443
1445 }
1446 else
1448 _("The command has no result, or the result has no columns.\n"));
1449 }
1450
1451 SetResultVariables(result, OK);
1452 ClearOrSaveResult(result);
1453
1454 return OK;
1455}
static bool PrintQueryResult(PGresult *result, bool last, const printQueryOpt *opt, FILE *printQueryFout, FILE *printStatusFout)
Definition: common.c:1039
static void SetResultVariables(PGresult *result, bool success)
Definition: common.c:476
static bool AcceptResult(const PGresult *result, bool show_error)
Definition: common.c:416
#define gettext_noop(x)
Definition: c.h:1195
void PQfreemem(void *ptr)
Definition: fe-exec.c:4043
Oid PQftype(const PGresult *res, int field_num)
Definition: fe-exec.c:3730
PGresult * PQdescribePrepared(PGconn *conn, const char *stmt)
Definition: fe-exec.c:2466
PGresult * PQprepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes)
Definition: fe-exec.c:2317
char * PQescapeLiteral(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:4376
int PQfmod(const PGresult *res, int field_num)
Definition: fe-exec.c:3752
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2273
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:122
#define INSTR_TIME_SUBTRACT(x, y)
Definition: instr_time.h:181
#define INSTR_TIME_GET_MILLISEC(t)
Definition: instr_time.h:191
#define INSTR_TIME_SET_ZERO(t)
Definition: instr_time.h:172
#define PQnfields
Definition: libpq-be-fe.h:252
#define PQfname
Definition: libpq-be-fe.h:256
static char * buf
Definition: pg_test_fsync.c:72
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:235
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:90
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:129
static int before(chr x, chr y)
Definition: regc_locale.c:488
FILE * queryFout
Definition: settings.h:105
const char * name

References _, AcceptResult(), appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), before(), buf, ClearOrSaveResult(), _psqlSettings::db, fprintf, gettext_noop, i, initPQExpBuffer(), INSTR_TIME_GET_MILLISEC, INSTR_TIME_SET_CURRENT, INSTR_TIME_SET_ZERO, INSTR_TIME_SUBTRACT, name, pg_log_info, PGRES_COMMAND_OK, PQclear, PQdescribePrepared(), PQerrorMessage(), PQescapeLiteral(), PQexec(), PQfmod(), PQfname, PQfreemem(), PQftype(), PQnfields, PQprepare(), PQresultStatus, printfPQExpBuffer(), PrintQueryResult(), pset, _psqlSettings::queryFout, SetResultVariables(), termPQExpBuffer(), and _psqlSettings::timing.

Referenced by SendQuery().

◆ discardAbortedPipelineResults()

static PGresult * discardAbortedPipelineResults ( void  )
static

Definition at line 1466 of file common.c.

1467{
1468 for (;;)
1469 {
1470 PGresult *res = PQgetResult(pset.db);
1471 ExecStatusType result_status = PQresultStatus(res);
1472
1473 if (result_status == PGRES_PIPELINE_SYNC)
1474 {
1475 /*
1476 * Found a synchronisation point. The sync counter is decremented
1477 * by the caller.
1478 */
1479 return res;
1480 }
1481 else if (res != NULL && result_status == PGRES_FATAL_ERROR)
1482 {
1483 /*
1484 * Found a FATAL error sent by the backend, and we cannot recover
1485 * from this state. Instead, return the last result and let the
1486 * outer loop handle it.
1487 */
1489
1490 /*
1491 * Fetch result to consume the end of the current query being
1492 * processed.
1493 */
1494 fatal_res = PQgetResult(pset.db);
1495 Assert(fatal_res == NULL);
1496 return res;
1497 }
1498 else if (res == NULL)
1499 {
1500 /* A query was processed, decrement the counters */
1505 }
1506
1507 if (pset.requested_results == 0)
1508 {
1509 /* We have read all the requested results, leave */
1510 return res;
1511 }
1512
1513 if (pset.available_results == 0 && pset.piped_syncs == 0)
1514 {
1515 /*
1516 * There are no more results to get and there is no
1517 * synchronisation point to stop at. This will leave the pipeline
1518 * in an aborted state.
1519 */
1520 return res;
1521 }
1522
1523 /*
1524 * An aborted pipeline will have either NULL results or results in an
1525 * PGRES_PIPELINE_ABORTED status.
1526 */
1527 Assert(res == NULL || result_status == PGRES_PIPELINE_ABORTED);
1528 PQclear(res);
1529 }
1530}
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:223
Assert(PointerIsAligned(start, uint64))
ExecStatusType
Definition: libpq-fe.h:123
int available_results
Definition: settings.h:128
int piped_syncs
Definition: settings.h:127
int requested_results
Definition: settings.h:129

References Assert(), _psqlSettings::available_results, _psqlSettings::db, PG_USED_FOR_ASSERTS_ONLY, PGRES_FATAL_ERROR, PGRES_PIPELINE_ABORTED, PGRES_PIPELINE_SYNC, _psqlSettings::piped_syncs, PQclear, PQgetResult, PQresultStatus, pset, and _psqlSettings::requested_results.

Referenced by ExecQueryAndProcessResults().

◆ ExecQueryAndProcessResults()

static int ExecQueryAndProcessResults ( const char *  query,
double *  elapsed_msec,
bool *  svpt_gone_p,
bool  is_watch,
int  min_rows,
const printQueryOpt opt,
FILE *  printQueryFout 
)
static

Definition at line 1556 of file common.c.

1560{
1561 bool timing = pset.timing;
1562 bool success = false;
1563 bool return_early = false;
1564 bool end_pipeline = false;
1566 after;
1567 PGresult *result;
1568 FILE *gfile_fout = NULL;
1569 bool gfile_is_pipe = false;
1570
1571 if (timing)
1573 else
1575
1576 switch (pset.send_mode)
1577 {
1582 break;
1584 success = PQsendPrepare(pset.db, pset.stmtName, query, 0, NULL);
1587 break;
1589 Assert(pset.stmtName == NULL);
1591 pset.bind_nparams, NULL,
1592 (const char *const *) pset.bind_params,
1593 NULL, NULL, 0);
1596 break;
1598 Assert(pset.stmtName != NULL);
1601 (const char *const *) pset.bind_params,
1602 NULL, NULL, 0);
1605 break;
1608 break;
1612 {
1613 /*
1614 * End of the pipeline, all queued commands need to be
1615 * processed.
1616 */
1617 end_pipeline = true;
1618 pset.piped_syncs++;
1619
1620 /*
1621 * The server will send a ReadyForQuery after a Sync is
1622 * processed, flushing all the results back to the client.
1623 */
1625 pset.piped_commands = 0;
1626
1627 /* We want to read all results */
1629 }
1630 break;
1634 {
1635 pset.piped_syncs++;
1636
1637 /*
1638 * The server will send a ReadyForQuery after a Sync is
1639 * processed, flushing all the results back to the client.
1640 */
1642 pset.piped_commands = 0;
1643 }
1644 break;
1645 case PSQL_SEND_FLUSH:
1647 break;
1651 {
1652 /*
1653 * With the flush request, all commands in the pipeline are
1654 * pushed and the server will flush the results back to the
1655 * client, making them available.
1656 */
1658 pset.piped_commands = 0;
1659 }
1660 break;
1662 if (pset.available_results == 0 && pset.piped_syncs == 0)
1663 {
1664 /*
1665 * If no sync or flush request were sent, PQgetResult() would
1666 * block as there are no results available. Forbid any
1667 * attempt to get pending results should we try to reach this
1668 * state.
1669 */
1670 pg_log_info("No pending results to get");
1671 success = false;
1673 }
1674 else
1675 {
1676 success = true;
1677
1678 /*
1679 * Cap requested_results to the maximum number of known
1680 * results.
1681 */
1682 if (pset.requested_results == 0 ||
1685 }
1686 break;
1687 case PSQL_SEND_QUERY:
1689 {
1691 0, NULL, NULL, NULL, NULL, 0);
1692 if (success)
1694 }
1695 else
1696 success = PQsendQuery(pset.db, query);
1697 break;
1698 }
1699
1700 if (!success)
1701 {
1702 const char *error = PQerrorMessage(pset.db);
1703
1704 if (strlen(error))
1705 pg_log_info("%s", error);
1706
1708
1710
1711 return -1;
1712 }
1713
1714 if (pset.requested_results == 0 && !end_pipeline &&
1716 {
1717 /*
1718 * We are in a pipeline and have not reached the pipeline end, or
1719 * there was no request to read pipeline results. Update the psql
1720 * variables tracking the pipeline activity and exit.
1721 */
1723 return 1;
1724 }
1725
1726 /*
1727 * Fetch the result in chunks if FETCH_COUNT is set, except when:
1728 *
1729 * * SHOW_ALL_RESULTS is false, since that requires us to complete the
1730 * query before we can tell if its results should be displayed.
1731 *
1732 * * We're doing \crosstab, which likewise needs to see all the rows at
1733 * once.
1734 *
1735 * * We're doing \gexec: we must complete the data fetch to make the
1736 * connection free for issuing the resulting commands.
1737 *
1738 * * We're doing \gset: only one result row is allowed anyway.
1739 *
1740 * * We're doing \watch: users probably don't want us to force use of the
1741 * pager for that, plus chunking could break the min_rows check.
1742 */
1745 !pset.gset_prefix && !is_watch)
1746 {
1748 pg_log_warning("fetching results in chunked mode failed");
1749 }
1750
1751 /*
1752 * If SIGINT is sent while the query is processing, the interrupt will be
1753 * consumed. The user's intention, though, is to cancel the entire watch
1754 * process, so detect a sent cancellation request and exit in this case.
1755 */
1756 if (is_watch && cancel_pressed)
1757 {
1759 return 0;
1760 }
1761
1762 /* first result */
1763 result = PQgetResult(pset.db);
1764 if (min_rows > 0 && PQntuples(result) < min_rows)
1765 {
1766 return_early = true;
1767 }
1768
1769 while (result != NULL)
1770 {
1771 ExecStatusType result_status;
1772 bool is_chunked_result = false;
1773 PGresult *next_result = NULL;
1774 bool last;
1775
1776 if (!AcceptResult(result, false))
1777 {
1778 /*
1779 * Some error occurred, either a server-side failure or a failure
1780 * to submit the command string. Record that.
1781 */
1782 const char *error = PQresultErrorMessage(result);
1783
1784 if (strlen(error))
1785 pg_log_info("%s", error);
1786
1788 if (!is_watch)
1789 SetResultVariables(result, false);
1790
1791 /* keep the result status before clearing it */
1792 result_status = PQresultStatus(result);
1793 ClearOrSaveResult(result);
1794 success = false;
1795
1796 if (result_status == PGRES_PIPELINE_ABORTED)
1797 pg_log_info("Pipeline aborted, command did not run");
1798
1799 /*
1800 * switch to next result
1801 */
1802 if (result_status == PGRES_COPY_BOTH ||
1803 result_status == PGRES_COPY_OUT ||
1804 result_status == PGRES_COPY_IN)
1805 {
1806 /*
1807 * For some obscure reason PQgetResult does *not* return a
1808 * NULL in copy cases despite the result having been cleared,
1809 * but keeps returning an "empty" result that we have to
1810 * ignore manually.
1811 */
1812 result = NULL;
1813 }
1814 else if ((end_pipeline || pset.requested_results > 0)
1816 {
1817 /*
1818 * Error within a pipeline. All commands are aborted until
1819 * the next synchronisation point. We need to consume all the
1820 * results until this synchronisation point, or stop when
1821 * there are no more result to discard.
1822 *
1823 * Checking the pipeline status is necessary for the case
1824 * where the connection was reset. The new connection is not
1825 * in any kind of pipeline state and thus has no result to
1826 * discard.
1827 */
1829 }
1830 else
1831 result = PQgetResult(pset.db);
1832
1833 /*
1834 * Get current timing measure in case an error occurs
1835 */
1836 if (timing)
1837 {
1840 *elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
1841 }
1842
1843 continue;
1844 }
1845 else if (svpt_gone_p && !*svpt_gone_p)
1846 {
1847 /*
1848 * Check if the user ran any command that would destroy our
1849 * internal savepoint: If the user did COMMIT AND CHAIN, RELEASE
1850 * or ROLLBACK, our savepoint is gone. If they issued a SAVEPOINT,
1851 * releasing ours would remove theirs.
1852 */
1853 const char *cmd = PQcmdStatus(result);
1854
1855 *svpt_gone_p = (strcmp(cmd, "COMMIT") == 0 ||
1856 strcmp(cmd, "SAVEPOINT") == 0 ||
1857 strcmp(cmd, "RELEASE") == 0 ||
1858 strcmp(cmd, "ROLLBACK") == 0);
1859 }
1860
1861 result_status = PQresultStatus(result);
1862
1863 /* must handle COPY before changing the current result */
1864 Assert(result_status != PGRES_COPY_BOTH);
1865 if (result_status == PGRES_COPY_IN ||
1866 result_status == PGRES_COPY_OUT)
1867 {
1868 FILE *copy_stream = NULL;
1869
1871 {
1872 /*
1873 * Running COPY within a pipeline can break the protocol
1874 * synchronisation in multiple ways, and psql shows its limits
1875 * when it comes to tracking this information.
1876 *
1877 * While in COPY mode, the backend process ignores additional
1878 * Sync messages and will not send the matching ReadyForQuery
1879 * expected by the frontend.
1880 *
1881 * Additionally, libpq automatically sends a Sync with the
1882 * Copy message, creating an unexpected synchronisation point.
1883 * A failure during COPY would leave the pipeline in an
1884 * aborted state while the backend would be in a clean state,
1885 * ready to process commands.
1886 *
1887 * Improving those issues would require modifications in how
1888 * libpq handles pipelines and COPY. Hence, for the time
1889 * being, we forbid the use of COPY within a pipeline,
1890 * aborting the connection to avoid an inconsistent state on
1891 * psql side if trying to use a COPY command.
1892 */
1893 pg_log_info("COPY in a pipeline is not supported, aborting connection");
1894 exit(EXIT_BADCONN);
1895 }
1896
1897 /*
1898 * For COPY OUT, direct the output to the default place (probably
1899 * a pager pipe) for \watch, or to pset.copyStream for \copy,
1900 * otherwise to pset.gfname if that's set, otherwise to
1901 * pset.queryFout.
1902 */
1903 if (result_status == PGRES_COPY_OUT)
1904 {
1905 if (is_watch)
1906 {
1907 /* invoked by \watch */
1908 copy_stream = printQueryFout ? printQueryFout : pset.queryFout;
1909 }
1910 else if (pset.copyStream)
1911 {
1912 /* invoked by \copy */
1913 copy_stream = pset.copyStream;
1914 }
1915 else if (pset.gfname)
1916 {
1917 /* COPY followed by \g filename or \g |program */
1918 success &= SetupGOutput(&gfile_fout, &gfile_is_pipe);
1919 if (gfile_fout)
1920 copy_stream = gfile_fout;
1921 }
1922 else
1923 {
1924 /* fall back to the generic query output stream */
1925 copy_stream = pset.queryFout;
1926 }
1927 }
1928
1929 /*
1930 * Even if the output stream could not be opened, we call
1931 * HandleCopyResult() with a NULL output stream to collect and
1932 * discard the COPY data.
1933 */
1934 success &= HandleCopyResult(&result, copy_stream);
1935 }
1936
1937 /* If we have a chunked result, collect and print all chunks */
1938 if (result_status == PGRES_TUPLES_CHUNK)
1939 {
1940 FILE *tuples_fout = printQueryFout ? printQueryFout : pset.queryFout;
1941 printQueryOpt my_popt = opt ? *opt : pset.popt;
1942 int64 total_tuples = 0;
1943 bool is_pager = false;
1944 int flush_error = 0;
1945
1946 /* initialize print options for partial table output */
1947 my_popt.topt.start_table = true;
1948 my_popt.topt.stop_table = false;
1949 my_popt.topt.prior_records = 0;
1950
1951 /* open \g file if needed */
1952 success &= SetupGOutput(&gfile_fout, &gfile_is_pipe);
1953 if (gfile_fout)
1954 tuples_fout = gfile_fout;
1955
1956 /* force use of pager for any chunked resultset going to stdout */
1957 if (success && tuples_fout == stdout)
1958 {
1959 tuples_fout = PageOutput(INT_MAX, &(my_popt.topt));
1960 is_pager = true;
1961 }
1962
1963 do
1964 {
1965 /*
1966 * Display the current chunk of results, unless the output
1967 * stream stopped working or we got canceled. We skip use of
1968 * PrintQueryResult and go directly to printQuery, so that we
1969 * can pass the correct is_pager value and because we don't
1970 * want PrintQueryStatus to happen yet. Above, we rejected
1971 * use of chunking for all cases in which PrintQueryResult
1972 * would send the result to someplace other than printQuery.
1973 */
1974 if (success && !flush_error && !cancel_pressed)
1975 {
1976 printQuery(result, &my_popt, tuples_fout, is_pager, pset.logfile);
1977 flush_error = fflush(tuples_fout);
1978 }
1979
1980 /* after the first result set, disallow header decoration */
1981 my_popt.topt.start_table = false;
1982
1983 /* count tuples before dropping the result */
1984 my_popt.topt.prior_records += PQntuples(result);
1985 total_tuples += PQntuples(result);
1986
1987 ClearOrSaveResult(result);
1988
1989 /* get the next result, loop if it's PGRES_TUPLES_CHUNK */
1990 result = PQgetResult(pset.db);
1991 } while (PQresultStatus(result) == PGRES_TUPLES_CHUNK);
1992
1993 /* We expect an empty PGRES_TUPLES_OK, else there's a problem */
1994 if (PQresultStatus(result) == PGRES_TUPLES_OK)
1995 {
1996 char buf[32];
1997
1998 Assert(PQntuples(result) == 0);
1999
2000 /* Display the footer using the empty result */
2001 if (success && !flush_error && !cancel_pressed)
2002 {
2003 my_popt.topt.stop_table = true;
2004 printQuery(result, &my_popt, tuples_fout, is_pager, pset.logfile);
2005 fflush(tuples_fout);
2006 }
2007
2008 if (is_pager)
2009 ClosePager(tuples_fout);
2010
2011 /*
2012 * It's possible the data is from a RETURNING clause, in which
2013 * case we need to print query status.
2014 */
2015 PrintQueryStatus(result, printQueryFout);
2016
2017 /*
2018 * We must do a fake SetResultVariables(), since we don't have
2019 * a PGresult corresponding to the whole query.
2020 */
2021 SetVariable(pset.vars, "ERROR", "false");
2022 SetVariable(pset.vars, "SQLSTATE", "00000");
2023 snprintf(buf, sizeof(buf), INT64_FORMAT, total_tuples);
2024 SetVariable(pset.vars, "ROW_COUNT", buf);
2025 /* Prevent SetResultVariables call below */
2026 is_chunked_result = true;
2027
2028 /* Clear the empty result so it isn't printed below */
2029 ClearOrSaveResult(result);
2030 result = NULL;
2031 }
2032 else
2033 {
2034 /* Probably an error report, so close the pager and print it */
2035 if (is_pager)
2036 ClosePager(tuples_fout);
2037
2038 success &= AcceptResult(result, true);
2039 /* SetResultVariables and ClearOrSaveResult happen below */
2040 }
2041 }
2042
2043 if (result_status == PGRES_PIPELINE_SYNC)
2044 {
2045 Assert(pset.piped_syncs > 0);
2046
2047 /*
2048 * Sync response, decrease the sync and requested_results
2049 * counters.
2050 */
2051 pset.piped_syncs--;
2053
2054 /*
2055 * After a synchronisation point, reset success state to print
2056 * possible successful results that will be processed after this.
2057 */
2058 success = true;
2059
2060 /*
2061 * If all syncs were processed and pipeline end was requested,
2062 * exit pipeline mode.
2063 */
2064 if (end_pipeline && pset.piped_syncs == 0)
2066 }
2067 else if (PQpipelineStatus(pset.db) != PQ_PIPELINE_OFF &&
2068 result_status != PGRES_PIPELINE_SYNC)
2069 {
2070 /*
2071 * In a pipeline with a non-sync response? Decrease the result
2072 * counters.
2073 */
2076 }
2077
2078 /*
2079 * Check PQgetResult() again. In the typical case of a single-command
2080 * string, it will return NULL. Otherwise, we'll have other results
2081 * to process. We need to do that to check whether this is the last.
2082 */
2084 next_result = PQgetResult(pset.db);
2085 else
2086 {
2087 /*
2088 * In pipeline mode, a NULL result indicates the end of the
2089 * current query being processed. Call PQgetResult() once to
2090 * consume this state.
2091 */
2092 if (result_status != PGRES_PIPELINE_SYNC)
2093 {
2094 next_result = PQgetResult(pset.db);
2095 Assert(next_result == NULL);
2096 }
2097
2098 /* Now, we can get the next result in the pipeline. */
2099 if (pset.requested_results > 0)
2100 next_result = PQgetResult(pset.db);
2101 }
2102
2103 last = (next_result == NULL);
2104
2105 /*
2106 * Update current timing measure.
2107 *
2108 * It will include the display of previous results, if any. This
2109 * cannot be helped because the server goes on processing further
2110 * queries anyway while the previous ones are being displayed. The
2111 * parallel execution of the client display hides the server time when
2112 * it is shorter.
2113 *
2114 * With combined queries, timing must be understood as an upper bound
2115 * of the time spent processing them.
2116 */
2117 if (timing)
2118 {
2121 *elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
2122 }
2123
2124 /*
2125 * This may or may not print something depending on settings.
2126 *
2127 * A pipeline sync will have a non-NULL result but does not have
2128 * anything to print, thus ignore results in this case.
2129 */
2130 if (result != NULL && result_status != PGRES_PIPELINE_SYNC)
2131 {
2132 /*
2133 * If results need to be printed into the file specified by \g,
2134 * open it, unless we already did. Note that when pset.gfname is
2135 * set, the passed-in value of printQueryFout is not used for
2136 * tuple output, but it's still used for status output.
2137 */
2138 FILE *tuples_fout = printQueryFout;
2139
2140 if (PQresultStatus(result) == PGRES_TUPLES_OK)
2141 success &= SetupGOutput(&gfile_fout, &gfile_is_pipe);
2142 if (gfile_fout)
2143 tuples_fout = gfile_fout;
2144 if (success)
2145 success &= PrintQueryResult(result, last, opt,
2146 tuples_fout, printQueryFout);
2147 }
2148
2149 /* set variables from last result, unless dealt with elsewhere */
2150 if (last && !is_watch && !is_chunked_result)
2151 SetResultVariables(result, success);
2152
2153 ClearOrSaveResult(result);
2154 result = next_result;
2155
2157 {
2158 /*
2159 * Outside of a pipeline, drop the next result, as well as any
2160 * others not yet read.
2161 *
2162 * Within a pipeline, we can let the outer loop handle this as an
2163 * aborted pipeline, which will discard then all the results.
2164 */
2165 ClearOrSaveResult(result);
2167 break;
2168 }
2169 }
2170
2171 /* close \g file if we opened it */
2172 CloseGOutput(gfile_fout, gfile_is_pipe);
2173
2174 if (end_pipeline)
2175 {
2176 /* after a pipeline is processed, pipeline piped_syncs should be 0 */
2177 Assert(pset.piped_syncs == 0);
2178 /* all commands have been processed */
2180 /* all results were read */
2182 }
2185
2186 /* may need this to recover from conn loss during COPY */
2187 if (!CheckConnection())
2188 return -1;
2189
2190 if (cancel_pressed || return_early)
2191 return 0;
2192
2193 return success ? 1 : -1;
2194}
static void PrintQueryStatus(PGresult *result, FILE *printQueryFout)
Definition: common.c:992
static void ClearOrSaveAllResults(void)
Definition: common.c:582
static PGresult * discardAbortedPipelineResults(void)
Definition: common.c:1466
static bool SetupGOutput(FILE **gfile_fout, bool *is_pipe)
Definition: common.c:90
static bool HandleCopyResult(PGresult **resultp, FILE *copystream)
Definition: common.c:937
static void CloseGOutput(FILE *gfile_fout, bool is_pipe)
Definition: common.c:110
static void SetPipelineVariables(void)
Definition: common.c:534
#define INT64_FORMAT
Definition: c.h:556
int64_t int64
Definition: c.h:535
PGpipelineStatus PQpipelineStatus(const PGconn *conn)
Definition: fe-connect.c:7723
int PQsendQueryParams(PGconn *conn, const char *command, int nParams, const Oid *paramTypes, const char *const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat)
Definition: fe-exec.c:1503
int PQflush(PGconn *conn)
Definition: fe-exec.c:4011
int PQexitPipelineMode(PGconn *conn)
Definition: fe-exec.c:3084
int PQenterPipelineMode(PGconn *conn)
Definition: fe-exec.c:3053
int PQsendClosePrepared(PGconn *conn, const char *stmt)
Definition: fe-exec.c:2567
int PQsendPipelineSync(PGconn *conn)
Definition: fe-exec.c:3293
int PQsendPrepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes)
Definition: fe-exec.c:1547
int PQsetChunkedRowsMode(PGconn *conn, int chunkSize)
Definition: fe-exec.c:1976
int PQsendQuery(PGconn *conn, const char *query)
Definition: fe-exec.c:1427
int PQpipelineSync(PGconn *conn)
Definition: fe-exec.c:3283
int PQsendQueryPrepared(PGconn *conn, const char *stmtName, int nParams, const char *const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat)
Definition: fe-exec.c:1644
int PQsendFlushRequest(PGconn *conn)
Definition: fe-exec.c:3382
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3549
FILE * PageOutput(int lines, const printTableOpt *topt)
Definition: print.c:3089
void ClosePager(FILE *pagerpipe)
Definition: print.c:3141
volatile sig_atomic_t cancel_pressed
Definition: print.c:43
static bool success
Definition: initdb.c:187
#define PQresultErrorMessage
#define PQcmdStatus
Definition: libpq-be-fe.h:250
#define PQntuples
Definition: libpq-be-fe.h:251
@ PGRES_COPY_BOTH
Definition: libpq-fe.h:137
@ PQ_PIPELINE_OFF
Definition: libpq-fe.h:187
#define pg_log_warning(...)
Definition: pgfnames.c:24
#define snprintf
Definition: port.h:239
printQueryOpt popt
Definition: settings.h:112
char * gset_prefix
Definition: settings.h:117
VariableSpace vars
Definition: settings.h:151
FILE * logfile
Definition: settings.h:149
bool show_all_results
Definition: settings.h:185
FILE * copyStream
Definition: settings.h:108
int piped_commands
Definition: settings.h:126
char * gfname
Definition: settings.h:114
int fetch_count
Definition: settings.h:172
bool gexec_flag
Definition: settings.h:119
bool crosstab_flag
Definition: settings.h:132
printTableOpt topt
Definition: print.h:185
bool start_table
Definition: print.h:127
unsigned long prior_records
Definition: print.h:130
bool stop_table
Definition: print.h:128
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:281

References AcceptResult(), Assert(), _psqlSettings::available_results, before(), _psqlSettings::bind_nparams, _psqlSettings::bind_params, buf, cancel_pressed, CheckConnection(), ClearOrSaveAllResults(), ClearOrSaveResult(), CloseGOutput(), ClosePager(), _psqlSettings::copyStream, _psqlSettings::crosstab_flag, _psqlSettings::db, discardAbortedPipelineResults(), error(), EXIT_BADCONN, _psqlSettings::fetch_count, _psqlSettings::gexec_flag, _psqlSettings::gfname, _psqlSettings::gset_prefix, HandleCopyResult(), INSTR_TIME_GET_MILLISEC, INSTR_TIME_SET_CURRENT, INSTR_TIME_SET_ZERO, INSTR_TIME_SUBTRACT, INT64_FORMAT, _psqlSettings::logfile, PageOutput(), pg_log_info, pg_log_warning, PGRES_COPY_BOTH, PGRES_COPY_IN, PGRES_COPY_OUT, PGRES_PIPELINE_ABORTED, PGRES_PIPELINE_SYNC, PGRES_TUPLES_CHUNK, PGRES_TUPLES_OK, _psqlSettings::piped_commands, _psqlSettings::piped_syncs, _psqlSettings::popt, PQ_PIPELINE_OFF, PQcmdStatus, PQenterPipelineMode(), PQerrorMessage(), PQexitPipelineMode(), PQflush(), PQgetResult, PQntuples, PQpipelineStatus(), PQpipelineSync(), PQresultErrorMessage, PQresultStatus, PQsendClosePrepared(), PQsendFlushRequest(), PQsendPipelineSync(), PQsendPrepare(), PQsendQuery(), PQsendQueryParams(), PQsendQueryPrepared(), PQsetChunkedRowsMode(), printQuery(), PrintQueryResult(), PrintQueryStatus(), printTableOpt::prior_records, pset, PSQL_SEND_END_PIPELINE_MODE, PSQL_SEND_EXTENDED_CLOSE, PSQL_SEND_EXTENDED_PARSE, PSQL_SEND_EXTENDED_QUERY_PARAMS, PSQL_SEND_EXTENDED_QUERY_PREPARED, PSQL_SEND_FLUSH, PSQL_SEND_FLUSH_REQUEST, PSQL_SEND_GET_RESULTS, PSQL_SEND_PIPELINE_SYNC, PSQL_SEND_QUERY, PSQL_SEND_START_PIPELINE_MODE, _psqlSettings::queryFout, _psqlSettings::requested_results, _psqlSettings::send_mode, SetPipelineVariables(), SetResultVariables(), SetupGOutput(), SetVariable(), _psqlSettings::show_all_results, snprintf, printTableOpt::start_table, generate_unaccent_rules::stdout, _psqlSettings::stmtName, printTableOpt::stop_table, success, _psqlSettings::timing, printQueryOpt::topt, and _psqlSettings::vars.

Referenced by PSQLexecWatch(), and SendQuery().

◆ ExecQueryTuples()

static bool ExecQueryTuples ( const PGresult result)
static

Definition at line 861 of file common.c.

862{
863 bool success = true;
864 int nrows = PQntuples(result);
865 int ncolumns = PQnfields(result);
866 int r,
867 c;
868
869 /*
870 * We must turn off gexec_flag to avoid infinite recursion.
871 */
872 pset.gexec_flag = false;
873
874 for (r = 0; r < nrows; r++)
875 {
876 for (c = 0; c < ncolumns; c++)
877 {
878 if (!PQgetisnull(result, r, c))
879 {
880 const char *query = PQgetvalue(result, r, c);
881
882 /* Abandon execution if cancel_pressed */
883 if (cancel_pressed)
884 goto loop_exit;
885
886 /*
887 * ECHO_ALL mode should echo these queries, but SendQuery
888 * assumes that MainLoop did that, so we have to do it here.
889 */
891 {
892 puts(query);
893 fflush(stdout);
894 }
895
896 if (!SendQuery(query))
897 {
898 /* Error - abandon execution if ON_ERROR_STOP */
899 success = false;
901 goto loop_exit;
902 }
903 }
904 }
905 }
906
907loop_exit:
908
909 /*
910 * Restore state. We know gexec_flag was on, else we'd not be here. (We
911 * also know it'll get turned off at end of command, but that's not ours
912 * to do here.)
913 */
914 pset.gexec_flag = true;
915
916 /* Return true if all queries were successful */
917 return success;
918}
bool SendQuery(const char *query)
Definition: common.c:1118
#define PQgetvalue
Definition: libpq-be-fe.h:253
#define PQgetisnull
Definition: libpq-be-fe.h:255
char * c
@ PSQL_ECHO_ALL
Definition: settings.h:46
bool on_error_stop
Definition: settings.h:166
PSQL_ECHO echo
Definition: settings.h:176
bool singlestep
Definition: settings.h:169

References cancel_pressed, _psqlSettings::echo, _psqlSettings::gexec_flag, _psqlSettings::on_error_stop, PQgetisnull, PQgetvalue, PQnfields, PQntuples, pset, PSQL_ECHO_ALL, SendQuery(), _psqlSettings::singlestep, generate_unaccent_rules::stdout, and success.

Referenced by PrintQueryResult().

◆ expand_tilde()

void expand_tilde ( char **  filename)

Definition at line 2576 of file common.c.

2577{
2578 if (!filename || !(*filename))
2579 return;
2580
2581 /*
2582 * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde
2583 * for short versions of long file names, though the tilde is usually
2584 * toward the end, not at the beginning.
2585 */
2586#ifndef WIN32
2587
2588 /* try tilde expansion */
2589 if (**filename == '~')
2590 {
2591 char *fn;
2592 char oldp,
2593 *p;
2594 struct passwd *pw;
2595 char home[MAXPGPATH];
2596
2597 fn = *filename;
2598 *home = '\0';
2599
2600 p = fn + 1;
2601 while (*p != '/' && *p != '\0')
2602 p++;
2603
2604 oldp = *p;
2605 *p = '\0';
2606
2607 if (*(fn + 1) == '\0')
2608 get_home_path(home); /* ~ or ~/ only */
2609 else if ((pw = getpwnam(fn + 1)) != NULL)
2610 strlcpy(home, pw->pw_dir, sizeof(home)); /* ~user */
2611
2612 *p = oldp;
2613 if (strlen(home) != 0)
2614 {
2615 char *newfn;
2616
2617 newfn = psprintf("%s%s", home, p);
2618 free(fn);
2619 *filename = newfn;
2620 }
2621 }
2622#endif
2623}
#define MAXPGPATH
static char * filename
Definition: pg_dumpall.c:120
bool get_home_path(char *ret_path)
Definition: path.c:1005
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43
static void * fn(void *arg)
Definition: thread-alloc.c:119

References filename, fn(), free, get_home_path(), MAXPGPATH, psprintf(), and strlcpy().

Referenced by exec_command_edit(), exec_command_g(), exec_command_include(), exec_command_lo(), exec_command_out(), exec_command_s(), exec_command_write(), initializeInput(), parse_slash_copy(), and process_psqlrc().

◆ get_conninfo_value()

char * get_conninfo_value ( const char *  keyword)

Definition at line 2540 of file common.c.

2541{
2543 PQconninfoOption *serviceopt = NULL;
2544 char *res = NULL;
2545
2546 if (pset.db == NULL)
2547 return NULL;
2548
2550 if (opts == NULL)
2551 return NULL;
2552
2553 for (PQconninfoOption *opt = opts; opt->keyword != NULL; ++opt)
2554 {
2555 if (strcmp(opt->keyword, keyword) == 0)
2556 {
2557 serviceopt = opt;
2558 break;
2559 }
2560 }
2561
2562 /* Take a copy of the value, as it is freed by PQconninfoFree(). */
2563 if (serviceopt && serviceopt->val != NULL)
2564 res = pg_strdup(serviceopt->val);
2566
2567 return res;
2568}
PQconninfoOption * PQconninfo(PGconn *conn)
Definition: fe-connect.c:7456
void PQconninfoFree(PQconninfoOption *connOptions)
Definition: fe-connect.c:7500
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static AmcheckOptions opts
Definition: pg_amcheck.c:112

References _psqlSettings::db, opts, pg_strdup(), PQconninfo(), PQconninfoFree(), pset, and _PQconninfoOption::val.

Referenced by SyncVariables().

◆ HandleCopyResult()

static bool HandleCopyResult ( PGresult **  resultp,
FILE *  copystream 
)
static

Definition at line 937 of file common.c.

938{
939 bool success;
940 PGresult *copy_result;
941 ExecStatusType result_status = PQresultStatus(*resultp);
942
943 Assert(result_status == PGRES_COPY_OUT ||
944 result_status == PGRES_COPY_IN);
945
947
948 if (result_status == PGRES_COPY_OUT)
949 {
951 copystream,
952 &copy_result)
953 && (copystream != NULL);
954
955 /*
956 * Suppress status printing if the report would go to the same place
957 * as the COPY data just went. Note this doesn't prevent error
958 * reporting, since handleCopyOut did that.
959 */
960 if (copystream == pset.queryFout)
961 {
962 PQclear(copy_result);
963 copy_result = NULL;
964 }
965 }
966 else
967 {
968 /* COPY IN */
969 /* Ignore the copystream argument passed to the function */
972 copystream,
973 PQbinaryTuples(*resultp),
974 &copy_result);
975 }
977
978 /*
979 * Replace the PGRES_COPY_OUT/IN result with COPY command's exit status,
980 * or with NULL if we want to suppress printing anything.
981 */
982 PQclear(*resultp);
983 *resultp = copy_result;
984
985 return success;
986}
bool handleCopyOut(PGconn *conn, FILE *copystream, PGresult **res)
Definition: copy.c:434
bool handleCopyIn(PGconn *conn, FILE *copystream, bool isbinary, PGresult **res)
Definition: copy.c:511
void SetCancelConn(PGconn *conn)
Definition: cancel.c:77
int PQbinaryTuples(const PGresult *res)
Definition: fe-exec.c:3508
FILE * cur_cmd_source
Definition: settings.h:138

References Assert(), _psqlSettings::copyStream, _psqlSettings::cur_cmd_source, _psqlSettings::db, handleCopyIn(), handleCopyOut(), PGRES_COPY_IN, PGRES_COPY_OUT, PQbinaryTuples(), PQclear, PQresultStatus, pset, _psqlSettings::queryFout, ResetCancelConn(), SetCancelConn(), and success.

Referenced by ExecQueryAndProcessResults().

◆ is_superuser()

bool is_superuser ( void  )

Definition at line 2480 of file common.c.

2481{
2482 const char *val;
2483
2484 if (!pset.db)
2485 return false;
2486
2487 val = PQparameterStatus(pset.db, "is_superuser");
2488
2489 if (val && strcmp(val, "on") == 0)
2490 return true;
2491
2492 return false;
2493}
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:7634
long val
Definition: informix.c:689

References _psqlSettings::db, PQparameterStatus(), pset, and val.

◆ NoticeProcessor()

void NoticeProcessor ( void *  arg,
const char *  message 
)

Definition at line 279 of file common.c.

280{
281 (void) arg; /* not used */
282 pg_log_info("%s", message);
283}
void * arg

References arg, and pg_log_info.

Referenced by do_connect(), and main().

◆ openQueryOutputFile()

bool openQueryOutputFile ( const char *  fname,
FILE **  fout,
bool *  is_pipe 
)

Definition at line 56 of file common.c.

57{
58 if (!fname || fname[0] == '\0')
59 {
60 *fout = stdout;
61 *is_pipe = false;
62 }
63 else if (*fname == '|')
64 {
65 fflush(NULL);
66 *fout = popen(fname + 1, "w");
67 *is_pipe = true;
68 }
69 else
70 {
71 *fout = fopen(fname, "w");
72 *is_pipe = false;
73 }
74
75 if (*fout == NULL)
76 {
77 pg_log_error("%s: %m", fname);
78 return false;
79 }
80
81 return true;
82}

References pg_log_error, and generate_unaccent_rules::stdout.

Referenced by setQFout(), and SetupGOutput().

◆ pipelineReset()

static void pipelineReset ( void  )
static

◆ PrintNotifications()

static void PrintNotifications ( void  )
static

Definition at line 740 of file common.c.

741{
742 PGnotify *notify;
743
745 while ((notify = PQnotifies(pset.db)) != NULL)
746 {
747 /* for backward compatibility, only show payload if nonempty */
748 if (notify->extra[0])
749 fprintf(pset.queryFout, _("Asynchronous notification \"%s\" with payload \"%s\" received from server process with PID %d.\n"),
750 notify->relname, notify->extra, notify->be_pid);
751 else
752 fprintf(pset.queryFout, _("Asynchronous notification \"%s\" received from server process with PID %d.\n"),
753 notify->relname, notify->be_pid);
754 fflush(pset.queryFout);
755 PQfreemem(notify);
757 }
758}
int PQconsumeInput(PGconn *conn)
Definition: fe-exec.c:1995
PGnotify * PQnotifies(PGconn *conn)
Definition: fe-exec.c:2678
int be_pid
Definition: libpq-fe.h:231
char * relname
Definition: libpq-fe.h:230
char * extra
Definition: libpq-fe.h:232

References _, pgNotify::be_pid, _psqlSettings::db, pgNotify::extra, fprintf, PQconsumeInput(), PQfreemem(), PQnotifies(), pset, _psqlSettings::queryFout, and pgNotify::relname.

Referenced by SendQuery().

◆ PrintQueryResult()

static bool PrintQueryResult ( PGresult result,
bool  last,
const printQueryOpt opt,
FILE *  printQueryFout,
FILE *  printStatusFout 
)
static

Definition at line 1039 of file common.c.

1042{
1043 bool success;
1044
1045 if (!result)
1046 return false;
1047
1048 switch (PQresultStatus(result))
1049 {
1050 case PGRES_TUPLES_OK:
1051 /* store or execute or print the data ... */
1052 if (last && pset.gset_prefix)
1053 success = StoreQueryTuple(result);
1054 else if (last && pset.gexec_flag)
1055 success = ExecQueryTuples(result);
1056 else if (last && pset.crosstab_flag)
1058 else if (last || pset.show_all_results)
1059 success = PrintQueryTuples(result, opt, printQueryFout);
1060 else
1061 success = true;
1062
1063 /*
1064 * If it's INSERT/UPDATE/DELETE/MERGE RETURNING, also print
1065 * status.
1066 */
1067 if (last || pset.show_all_results)
1068 PrintQueryStatus(result, printStatusFout);
1069
1070 break;
1071
1072 case PGRES_COMMAND_OK:
1073 if (last || pset.show_all_results)
1074 PrintQueryStatus(result, printStatusFout);
1075 success = true;
1076 break;
1077
1078 case PGRES_EMPTY_QUERY:
1079 success = true;
1080 break;
1081
1082 case PGRES_COPY_OUT:
1083 case PGRES_COPY_IN:
1084 /* nothing to do here: already processed */
1085 success = true;
1086 break;
1087
1089 case PGRES_BAD_RESPONSE:
1091 case PGRES_FATAL_ERROR:
1092 success = false;
1093 break;
1094
1095 default:
1096 success = false;
1097 pg_log_error("unexpected PQresultStatus: %d",
1098 PQresultStatus(result));
1099 break;
1100 }
1101
1102 return success;
1103}
static bool ExecQueryTuples(const PGresult *result)
Definition: common.c:861
static bool StoreQueryTuple(const PGresult *result)
Definition: common.c:797
static bool PrintQueryTuples(const PGresult *result, const printQueryOpt *opt, FILE *printQueryFout)
Definition: common.c:773
bool PrintResultInCrosstab(const PGresult *res)
Definition: crosstabview.c:104

References _psqlSettings::crosstab_flag, ExecQueryTuples(), _psqlSettings::gexec_flag, _psqlSettings::gset_prefix, pg_log_error, PGRES_BAD_RESPONSE, PGRES_COMMAND_OK, PGRES_COPY_IN, PGRES_COPY_OUT, PGRES_EMPTY_QUERY, PGRES_FATAL_ERROR, PGRES_NONFATAL_ERROR, PGRES_PIPELINE_ABORTED, PGRES_TUPLES_OK, PQresultStatus, PrintQueryStatus(), PrintQueryTuples(), PrintResultInCrosstab(), pset, _psqlSettings::show_all_results, StoreQueryTuple(), and success.

Referenced by DescribeQuery(), and ExecQueryAndProcessResults().

◆ PrintQueryStatus()

static void PrintQueryStatus ( PGresult result,
FILE *  printQueryFout 
)
static

Definition at line 992 of file common.c.

993{
994 char buf[16];
995 const char *cmdstatus = PQcmdStatus(result);
996 FILE *fout = printQueryFout ? printQueryFout : pset.queryFout;
997
998 /* Do nothing if it's a TUPLES_OK result that isn't from RETURNING */
999 if (PQresultStatus(result) == PGRES_TUPLES_OK)
1000 {
1001 if (!(strncmp(cmdstatus, "INSERT", 6) == 0 ||
1002 strncmp(cmdstatus, "UPDATE", 6) == 0 ||
1003 strncmp(cmdstatus, "DELETE", 6) == 0 ||
1004 strncmp(cmdstatus, "MERGE", 5) == 0))
1005 return;
1006 }
1007
1008 if (!pset.quiet)
1009 {
1011 {
1012 fputs("<p>", fout);
1013 html_escaped_print(cmdstatus, fout);
1014 fputs("</p>\n", fout);
1015 }
1016 else
1017 fprintf(fout, "%s\n", cmdstatus);
1018 fflush(fout);
1019 }
1020
1021 if (pset.logfile)
1022 fprintf(pset.logfile, "%s\n", cmdstatus);
1023
1024 snprintf(buf, sizeof(buf), "%u", (unsigned int) PQoidValue(result));
1025 SetVariable(pset.vars, "LASTOID", buf);
1026}
Oid PQoidValue(const PGresult *res)
Definition: fe-exec.c:3804
void html_escaped_print(const char *in, FILE *fout)
Definition: print.c:1952
@ PRINT_HTML
Definition: print.h:34
enum printFormat format
Definition: print.h:113

References buf, printTableOpt::format, fprintf, html_escaped_print(), _psqlSettings::logfile, PGRES_TUPLES_OK, _psqlSettings::popt, PQcmdStatus, PQoidValue(), PQresultStatus, PRINT_HTML, pset, _psqlSettings::queryFout, _psqlSettings::quiet, SetVariable(), snprintf, printQueryOpt::topt, and _psqlSettings::vars.

Referenced by ExecQueryAndProcessResults(), and PrintQueryResult().

◆ PrintQueryTuples()

static bool PrintQueryTuples ( const PGresult result,
const printQueryOpt opt,
FILE *  printQueryFout 
)
static

Definition at line 773 of file common.c.

775{
776 bool ok = true;
777 FILE *fout = printQueryFout ? printQueryFout : pset.queryFout;
778
779 printQuery(result, opt ? opt : &pset.popt, fout, false, pset.logfile);
780 fflush(fout);
781 if (ferror(fout))
782 {
783 pg_log_error("could not print result table: %m");
784 ok = false;
785 }
786
787 return ok;
788}

References _psqlSettings::logfile, pg_log_error, _psqlSettings::popt, printQuery(), pset, and _psqlSettings::queryFout.

Referenced by PrintQueryResult().

◆ PrintTiming()

static void PrintTiming ( double  elapsed_msec)
static

Definition at line 596 of file common.c.

597{
598 double seconds;
599 double minutes;
600 double hours;
601 double days;
602
603 if (elapsed_msec < 1000.0)
604 {
605 /* This is the traditional (pre-v10) output format */
606 printf(_("Time: %.3f ms\n"), elapsed_msec);
607 return;
608 }
609
610 /*
611 * Note: we could print just seconds, in a format like %06.3f, when the
612 * total is less than 1min. But that's hard to interpret unless we tack
613 * on "s" or otherwise annotate it. Forcing the display to include
614 * minutes seems like a better solution.
615 */
616 seconds = elapsed_msec / 1000.0;
617 minutes = floor(seconds / 60.0);
618 seconds -= 60.0 * minutes;
619 if (minutes < 60.0)
620 {
621 printf(_("Time: %.3f ms (%02d:%06.3f)\n"),
622 elapsed_msec, (int) minutes, seconds);
623 return;
624 }
625
626 hours = floor(minutes / 60.0);
627 minutes -= 60.0 * hours;
628 if (hours < 24.0)
629 {
630 printf(_("Time: %.3f ms (%02d:%02d:%06.3f)\n"),
631 elapsed_msec, (int) hours, (int) minutes, seconds);
632 return;
633 }
634
635 days = floor(hours / 24.0);
636 hours -= 24.0 * days;
637 printf(_("Time: %.3f ms (%.0f d %02d:%02d:%06.3f)\n"),
638 elapsed_msec, days, (int) hours, (int) minutes, seconds);
639}
const char *const days[]
Definition: datetime.c:84
#define printf(...)
Definition: port.h:245

References _, days, and printf.

Referenced by PSQLexecWatch(), and SendQuery().

◆ psql_cancel_callback()

static void psql_cancel_callback ( void  )
static

Definition at line 309 of file common.c.

310{
311#ifndef WIN32
312 /* if we are waiting for input, longjmp out of it */
314 {
316 siglongjmp(sigint_interrupt_jmp, 1);
317 }
318#endif
319
320 /* else, set cancel flag to stop any long-running loops */
321 cancel_pressed = true;
322}
volatile sig_atomic_t sigint_interrupt_enabled
Definition: common.c:304
sigjmp_buf sigint_interrupt_jmp
Definition: common.c:306

References cancel_pressed, sigint_interrupt_enabled, and sigint_interrupt_jmp.

Referenced by psql_setup_cancel_handler().

◆ psql_get_variable()

char * psql_get_variable ( const char *  varname,
PsqlScanQuoteType  quote,
void *  passthrough 
)

Definition at line 188 of file common.c.

190{
191 char *result = NULL;
192 const char *value;
193
194 /* In an inactive \if branch, suppress all variable substitutions */
195 if (passthrough && !conditional_active((ConditionalStack) passthrough))
196 return NULL;
197
198 value = GetVariable(pset.vars, varname);
199 if (!value)
200 return NULL;
201
202 switch (quote)
203 {
204 case PQUOTE_PLAIN:
205 result = pg_strdup(value);
206 break;
208 case PQUOTE_SQL_IDENT:
209 {
210 /*
211 * For these cases, we use libpq's quoting functions, which
212 * assume the string is in the connection's client encoding.
213 */
214 char *escaped_value;
215
216 if (!pset.db)
217 {
218 pg_log_error("cannot escape without active connection");
219 return NULL;
220 }
221
222 if (quote == PQUOTE_SQL_LITERAL)
223 escaped_value =
224 PQescapeLiteral(pset.db, value, strlen(value));
225 else
226 escaped_value =
228
229 if (escaped_value == NULL)
230 {
231 const char *error = PQerrorMessage(pset.db);
232
233 pg_log_info("%s", error);
234 return NULL;
235 }
236
237 /*
238 * Rather than complicate the lexer's API with a notion of
239 * which free() routine to use, just pay the price of an extra
240 * strdup().
241 */
242 result = pg_strdup(escaped_value);
243 PQfreemem(escaped_value);
244 break;
245 }
246 case PQUOTE_SHELL_ARG:
247 {
248 /*
249 * For this we use appendShellStringNoError, which is
250 * encoding-agnostic, which is fine since the shell probably
251 * is too. In any case, the only special character is "'",
252 * which is not known to appear in valid multibyte characters.
253 */
255
258 {
259 pg_log_error("shell command argument contains a newline or carriage return: \"%s\"",
260 value);
261 free(buf.data);
262 return NULL;
263 }
264 result = buf.data;
265 break;
266 }
267
268 /* No default: we want a compiler warning for missing cases */
269 }
270
271 return result;
272}
bool conditional_active(ConditionalStack cstack)
Definition: conditional.c:140
char * PQescapeIdentifier(PGconn *conn, const char *str, size_t len)
Definition: fe-exec.c:4382
static struct @169 value
@ PQUOTE_SQL_LITERAL
Definition: psqlscan.h:55
@ PQUOTE_PLAIN
Definition: psqlscan.h:54
@ PQUOTE_SHELL_ARG
Definition: psqlscan.h:57
@ PQUOTE_SQL_IDENT
Definition: psqlscan.h:56
bool appendShellStringNoError(PQExpBuffer buf, const char *str)
Definition: string_utils.c:594
const char * GetVariable(VariableSpace space, const char *name)
Definition: variables.c:73

References appendShellStringNoError(), buf, conditional_active(), _psqlSettings::db, error(), free, GetVariable(), initPQExpBuffer(), pg_log_error, pg_log_info, pg_strdup(), PQerrorMessage(), PQescapeIdentifier(), PQescapeLiteral(), PQfreemem(), PQUOTE_PLAIN, PQUOTE_SHELL_ARG, PQUOTE_SQL_IDENT, PQUOTE_SQL_LITERAL, pset, value, and _psqlSettings::vars.

◆ psql_setup_cancel_handler()

void psql_setup_cancel_handler ( void  )

Definition at line 325 of file common.c.

326{
328}
static void psql_cancel_callback(void)
Definition: common.c:309
void setup_cancel_handler(void(*query_cancel_callback)(void))
Definition: cancel.c:183

References psql_cancel_callback(), and setup_cancel_handler().

Referenced by main().

◆ PSQLexec()

PGresult * PSQLexec ( const char *  query)

Definition at line 655 of file common.c.

656{
657 PGresult *res;
658
659 if (!pset.db)
660 {
661 pg_log_error("You are currently not connected to a database.");
662 return NULL;
663 }
664
666 {
667 printf(_("/******** QUERY *********/\n"
668 "%s\n"
669 "/************************/\n\n"), query);
670 fflush(stdout);
671 if (pset.logfile)
672 {
674 _("/******** QUERY *********/\n"
675 "%s\n"
676 "/************************/\n\n"), query);
677 fflush(pset.logfile);
678 }
679
681 return NULL;
682 }
683
685
686 res = PQexec(pset.db, query);
687
689
690 if (!AcceptResult(res, true))
691 {
693 res = NULL;
694 }
695
696 return res;
697}
@ PSQL_ECHO_HIDDEN_NOEXEC
Definition: settings.h:53
@ PSQL_ECHO_HIDDEN_OFF
Definition: settings.h:51
PSQL_ECHO_HIDDEN echo_hidden
Definition: settings.h:177

References _, AcceptResult(), ClearOrSaveResult(), _psqlSettings::db, _psqlSettings::echo_hidden, fprintf, _psqlSettings::logfile, pg_log_error, PQexec(), printf, pset, PSQL_ECHO_HIDDEN_NOEXEC, PSQL_ECHO_HIDDEN_OFF, ResetCancelConn(), SetCancelConn(), and generate_unaccent_rules::stdout.

Referenced by add_tablespace_footer(), addFooterToPublicationDesc(), describeAccessMethods(), describeAggregates(), describeConfigurationParameters(), describeFunctions(), describeOneTableDetails(), describeOneTSConfig(), describeOneTSParser(), describeOperators(), describePublications(), describeRoleGrants(), describeRoles(), describeSubscriptions(), describeTableDetails(), describeTablespaces(), describeTypes(), do_lo_import(), exec_command_password(), fail_lo_xact(), finish_lo_xact(), listAllDbs(), listCasts(), listCollations(), listConversions(), listDbRoleSettings(), listDefaultACLs(), listDomains(), listEventTriggers(), listExtendedStats(), listExtensionContents(), listExtensions(), listForeignDataWrappers(), listForeignServers(), listForeignTables(), listLanguages(), listLargeObjects(), listOneExtensionContents(), listOperatorClasses(), listOperatorFamilies(), listOpFamilyFunctions(), listOpFamilyOperators(), listPartitionedTables(), listPublications(), listSchemas(), listTables(), listTSConfigs(), listTSConfigsVerbose(), listTSDictionaries(), listTSParsers(), listTSParsersVerbose(), listTSTemplates(), listUserMappings(), main(), objectDescription(), permissionsList(), and start_lo_xact().

◆ PSQLexecWatch()

int PSQLexecWatch ( const char *  query,
const printQueryOpt opt,
FILE *  printQueryFout,
int  min_rows 
)

Definition at line 710 of file common.c.

711{
712 bool timing = pset.timing;
713 double elapsed_msec = 0;
714 int res;
715
716 if (!pset.db)
717 {
718 pg_log_error("You are currently not connected to a database.");
719 return 0;
720 }
721
723
724 res = ExecQueryAndProcessResults(query, &elapsed_msec, NULL, true, min_rows, opt, printQueryFout);
725
727
728 /* Possible microtiming output */
729 if (timing)
730 PrintTiming(elapsed_msec);
731
732 return res;
733}
static int ExecQueryAndProcessResults(const char *query, double *elapsed_msec, bool *svpt_gone_p, bool is_watch, int min_rows, const printQueryOpt *opt, FILE *printQueryFout)
Definition: common.c:1556
static void PrintTiming(double elapsed_msec)
Definition: common.c:596

References _psqlSettings::db, ExecQueryAndProcessResults(), pg_log_error, PrintTiming(), pset, ResetCancelConn(), SetCancelConn(), and _psqlSettings::timing.

Referenced by do_watch().

◆ recognized_connection_string()

static bool recognized_connection_string ( const char *  connstr)

Definition at line 2704 of file common.c.

2705{
2706 return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL;
2707}
static int uri_prefix_length(const char *connstr)
Definition: common.c:2634
static const char * connstr
Definition: pg_dumpall.c:82

References connstr, and uri_prefix_length().

Referenced by do_connect().

◆ SendQuery()

bool SendQuery ( const char *  query)

Definition at line 1118 of file common.c.

1119{
1120 bool timing = pset.timing;
1121 PGTransactionStatusType transaction_status;
1122 double elapsed_msec = 0;
1123 bool OK = false;
1124 int i;
1125 bool on_error_rollback_savepoint = false;
1126 bool svpt_gone = false;
1127
1128 if (!pset.db)
1129 {
1130 pg_log_error("You are currently not connected to a database.");
1131 goto sendquery_cleanup;
1132 }
1133
1134 if (pset.singlestep)
1135 {
1136 char buf[3];
1137
1138 fflush(stderr);
1139 printf(_("/**(Single step mode: verify command)******************************************/\n"
1140 "%s\n"
1141 "/**(press return to proceed or enter x and return to cancel)*******************/\n"),
1142 query);
1143 fflush(stdout);
1144 if (fgets(buf, sizeof(buf), stdin) != NULL)
1145 if (buf[0] == 'x')
1146 goto sendquery_cleanup;
1147 if (cancel_pressed)
1148 goto sendquery_cleanup;
1149 }
1150 else if (pset.echo == PSQL_ECHO_QUERIES)
1151 {
1152 puts(query);
1153 fflush(stdout);
1154 }
1155
1156 if (pset.logfile)
1157 {
1159 _("/******** QUERY *********/\n"
1160 "%s\n"
1161 "/************************/\n\n"), query);
1162 fflush(pset.logfile);
1163 }
1164
1166
1167 transaction_status = PQtransactionStatus(pset.db);
1168
1169 if (transaction_status == PQTRANS_IDLE &&
1170 !pset.autocommit &&
1171 !command_no_begin(query))
1172 {
1173 PGresult *result;
1174
1175 result = PQexec(pset.db, "BEGIN");
1176 if (PQresultStatus(result) != PGRES_COMMAND_OK)
1177 {
1179 ClearOrSaveResult(result);
1180 goto sendquery_cleanup;
1181 }
1182 ClearOrSaveResult(result);
1183 transaction_status = PQtransactionStatus(pset.db);
1184 }
1185
1186 if (transaction_status == PQTRANS_INTRANS &&
1190 {
1191 PGresult *result;
1192
1193 result = PQexec(pset.db, "SAVEPOINT pg_psql_temporary_savepoint");
1194 if (PQresultStatus(result) != PGRES_COMMAND_OK)
1195 {
1197 ClearOrSaveResult(result);
1198 goto sendquery_cleanup;
1199 }
1200 ClearOrSaveResult(result);
1201 on_error_rollback_savepoint = true;
1202 }
1203
1204 if (pset.gdesc_flag)
1205 {
1206 /* Describe query's result columns, without executing it */
1207 OK = DescribeQuery(query, &elapsed_msec);
1208 }
1209 else
1210 {
1211 /* Default fetch-and-print mode */
1212 OK = (ExecQueryAndProcessResults(query, &elapsed_msec, &svpt_gone, false, 0, NULL, NULL) > 0);
1213 }
1214
1215 if (!OK && pset.echo == PSQL_ECHO_ERRORS)
1216 pg_log_info("STATEMENT: %s", query);
1217
1218 /* If we made a temporary savepoint, possibly release/rollback */
1219 if (on_error_rollback_savepoint)
1220 {
1221 const char *svptcmd = NULL;
1222
1223 transaction_status = PQtransactionStatus(pset.db);
1224
1225 switch (transaction_status)
1226 {
1227 case PQTRANS_INERROR:
1228 /* We always rollback on an error */
1229 svptcmd = "ROLLBACK TO pg_psql_temporary_savepoint";
1230 break;
1231
1232 case PQTRANS_IDLE:
1233 /* If they are no longer in a transaction, then do nothing */
1234 break;
1235
1236 case PQTRANS_INTRANS:
1237
1238 /*
1239 * Release our savepoint, but do nothing if they are messing
1240 * with savepoints themselves
1241 */
1242 if (!svpt_gone)
1243 svptcmd = "RELEASE pg_psql_temporary_savepoint";
1244 break;
1245
1246 case PQTRANS_ACTIVE:
1247 case PQTRANS_UNKNOWN:
1248 default:
1249 OK = false;
1250 /* PQTRANS_UNKNOWN is expected given a broken connection. */
1251 if (transaction_status != PQTRANS_UNKNOWN || ConnectionUp())
1252 pg_log_error("unexpected transaction status (%d)",
1253 transaction_status);
1254 break;
1255 }
1256
1257 if (svptcmd)
1258 {
1259 PGresult *svptres;
1260
1261 svptres = PQexec(pset.db, svptcmd);
1262 if (PQresultStatus(svptres) != PGRES_COMMAND_OK)
1263 {
1265 ClearOrSaveResult(svptres);
1266 OK = false;
1267
1268 goto sendquery_cleanup;
1269 }
1270 PQclear(svptres);
1271 }
1272 }
1273
1274 /* Possible microtiming output */
1275 if (timing)
1276 PrintTiming(elapsed_msec);
1277
1278 /* check for events that may occur during query execution */
1279
1281 PQclientEncoding(pset.db) >= 0)
1282 {
1283 /* track effects of SET CLIENT_ENCODING */
1286 SetVariable(pset.vars, "ENCODING",
1288 }
1289
1291
1292 /* perform cleanup that should occur after any attempted query */
1293
1294sendquery_cleanup:
1295
1296 /* global cancellation reset */
1298
1299 /* reset \g's output-to-filename trigger */
1300 if (pset.gfname)
1301 {
1302 free(pset.gfname);
1303 pset.gfname = NULL;
1304 }
1305
1306 /* restore print settings if \g changed them */
1307 if (pset.gsavepopt)
1308 {
1310 pset.gsavepopt = NULL;
1311 }
1312
1313 /* clean up after extended protocol queries */
1315
1316 /* reset \gset trigger */
1317 if (pset.gset_prefix)
1318 {
1320 pset.gset_prefix = NULL;
1321 }
1322
1323 /* reset \gdesc trigger */
1324 pset.gdesc_flag = false;
1325
1326 /* reset \gexec trigger */
1327 pset.gexec_flag = false;
1328
1329 /* reset \crosstabview trigger */
1330 pset.crosstab_flag = false;
1331 for (i = 0; i < lengthof(pset.ctv_args); i++)
1332 {
1334 pset.ctv_args[i] = NULL;
1335 }
1336
1337 return OK;
1338}
static bool DescribeQuery(const char *query, double *elapsed_msec)
Definition: common.c:1350
static void PrintNotifications(void)
Definition: common.c:740
void clean_extended_state(void)
Definition: common.c:2660
static bool command_no_begin(const char *query)
Definition: common.c:2265
#define lengthof(array)
Definition: c.h:787
void restorePsetInfo(printQueryOpt *popt, printQueryOpt *save)
Definition: command.c:5665
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:7624
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:7769
void pg_free(void *ptr)
Definition: fe_memutils.c:105
PGTransactionStatusType
Definition: libpq-fe.h:146
@ PQTRANS_INTRANS
Definition: libpq-fe.h:149
@ PQTRANS_IDLE
Definition: libpq-fe.h:147
@ PQTRANS_ACTIVE
Definition: libpq-fe.h:148
@ PQTRANS_UNKNOWN
Definition: libpq-fe.h:151
@ PQTRANS_INERROR
Definition: libpq-fe.h:150
#define pg_encoding_to_char
Definition: pg_wchar.h:630
@ PSQL_ERROR_ROLLBACK_ON
Definition: settings.h:60
@ PSQL_ERROR_ROLLBACK_OFF
Definition: settings.h:58
@ PSQL_ECHO_ERRORS
Definition: settings.h:45
@ PSQL_ECHO_QUERIES
Definition: settings.h:44
PSQL_ERROR_ROLLBACK on_error_rollback
Definition: settings.h:178
bool autocommit
Definition: settings.h:165
char * ctv_args[4]
Definition: settings.h:133
printQueryOpt * gsavepopt
Definition: settings.h:115
bool gdesc_flag
Definition: settings.h:118
int encoding
Definition: print.h:138

References _, _psqlSettings::autocommit, buf, cancel_pressed, clean_extended_state(), ClearOrSaveResult(), command_no_begin(), ConnectionUp(), _psqlSettings::crosstab_flag, _psqlSettings::ctv_args, _psqlSettings::cur_cmd_interactive, _psqlSettings::db, DescribeQuery(), _psqlSettings::echo, _psqlSettings::encoding, printTableOpt::encoding, ExecQueryAndProcessResults(), fprintf, free, _psqlSettings::gdesc_flag, _psqlSettings::gexec_flag, _psqlSettings::gfname, _psqlSettings::gsavepopt, _psqlSettings::gset_prefix, i, lengthof, _psqlSettings::logfile, _psqlSettings::on_error_rollback, pg_encoding_to_char, pg_free(), pg_log_error, pg_log_info, PGRES_COMMAND_OK, _psqlSettings::popt, PQclear, PQclientEncoding(), PQerrorMessage(), PQexec(), PQresultStatus, PQTRANS_ACTIVE, PQTRANS_IDLE, PQTRANS_INERROR, PQTRANS_INTRANS, PQTRANS_UNKNOWN, PQtransactionStatus(), printf, PrintNotifications(), PrintTiming(), pset, PSQL_ECHO_ERRORS, PSQL_ECHO_QUERIES, PSQL_ERROR_ROLLBACK_OFF, PSQL_ERROR_ROLLBACK_ON, ResetCancelConn(), restorePsetInfo(), SetCancelConn(), SetVariable(), _psqlSettings::singlestep, generate_unaccent_rules::stdout, _psqlSettings::timing, printQueryOpt::topt, and _psqlSettings::vars.

Referenced by do_copy(), ExecQueryTuples(), main(), and MainLoop().

◆ session_username()

const char * session_username ( void  )

Definition at line 2520 of file common.c.

2521{
2522 const char *val;
2523
2524 if (!pset.db)
2525 return NULL;
2526
2527 val = PQparameterStatus(pset.db, "session_authorization");
2528 if (val)
2529 return val;
2530 else
2531 return PQuser(pset.db);
2532}
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:7521

References _psqlSettings::db, PQparameterStatus(), PQuser(), pset, and val.

Referenced by get_prompt().

◆ SetPipelineVariables()

static void SetPipelineVariables ( void  )
static

Definition at line 534 of file common.c.

535{
536 char buf[32];
537
538 snprintf(buf, sizeof(buf), "%d", pset.piped_syncs);
539 SetVariable(pset.vars, "PIPELINE_SYNC_COUNT", buf);
540 snprintf(buf, sizeof(buf), "%d", pset.piped_commands);
541 SetVariable(pset.vars, "PIPELINE_COMMAND_COUNT", buf);
542 snprintf(buf, sizeof(buf), "%d", pset.available_results);
543 SetVariable(pset.vars, "PIPELINE_RESULT_COUNT", buf);
544}

References _psqlSettings::available_results, buf, _psqlSettings::piped_commands, _psqlSettings::piped_syncs, pset, SetVariable(), snprintf, and _psqlSettings::vars.

Referenced by ExecQueryAndProcessResults().

◆ setQFout()

bool setQFout ( const char *  fname)

Definition at line 144 of file common.c.

145{
146 FILE *fout;
147 bool is_pipe;
148
149 /* First make sure we can open the new output file/pipe */
150 if (!openQueryOutputFile(fname, &fout, &is_pipe))
151 return false;
152
153 /* Close old file/pipe */
154 if (pset.queryFout && pset.queryFout != stdout && pset.queryFout != stderr)
155 {
158 else
159 fclose(pset.queryFout);
160 }
161
162 pset.queryFout = fout;
163 pset.queryFoutPipe = is_pipe;
164
165 /* Adjust SIGPIPE handling appropriately: ignore signal if is_pipe */
166 set_sigpipe_trap_state(is_pipe);
168
169 return true;
170}
bool openQueryOutputFile(const char *fname, FILE **fout, bool *is_pipe)
Definition: common.c:56
void set_sigpipe_trap_state(bool ignore)
Definition: print.c:3075
bool queryFoutPipe
Definition: settings.h:106

References openQueryOutputFile(), pset, _psqlSettings::queryFout, _psqlSettings::queryFoutPipe, restore_sigpipe_trap(), set_sigpipe_trap_state(), SetShellResultVariables(), and generate_unaccent_rules::stdout.

Referenced by exec_command_out(), main(), and parse_psql_options().

◆ SetResultVariables()

static void SetResultVariables ( PGresult result,
bool  success 
)
static

Definition at line 476 of file common.c.

477{
478 if (success)
479 {
480 const char *ntuples = PQcmdTuples(result);
481
482 SetVariable(pset.vars, "ERROR", "false");
483 SetVariable(pset.vars, "SQLSTATE", "00000");
484 SetVariable(pset.vars, "ROW_COUNT", *ntuples ? ntuples : "0");
485 }
486 else
487 {
488 const char *code = PQresultErrorField(result, PG_DIAG_SQLSTATE);
489 const char *mesg = PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY);
490
491 SetVariable(pset.vars, "ERROR", "true");
492
493 /*
494 * If there is no SQLSTATE code, use an empty string. This can happen
495 * for libpq-detected errors (e.g., lost connection, ENOMEM).
496 */
497 if (code == NULL)
498 code = "";
499 SetVariable(pset.vars, "SQLSTATE", code);
500 SetVariable(pset.vars, "ROW_COUNT", "0");
501 SetVariable(pset.vars, "LAST_ERROR_SQLSTATE", code);
502 SetVariable(pset.vars, "LAST_ERROR_MESSAGE", mesg ? mesg : "");
503 }
504}
#define PQresultErrorField
Definition: libpq-be-fe.h:249
#define PQcmdTuples
Definition: libpq-be-fe.h:257
#define PG_DIAG_SQLSTATE
Definition: postgres_ext.h:57
#define PG_DIAG_MESSAGE_PRIMARY
Definition: postgres_ext.h:58

References PG_DIAG_MESSAGE_PRIMARY, PG_DIAG_SQLSTATE, PQcmdTuples, PQresultErrorField, pset, SetVariable(), success, and _psqlSettings::vars.

Referenced by DescribeQuery(), and ExecQueryAndProcessResults().

◆ SetShellResultVariables()

void SetShellResultVariables ( int  wait_result)

Definition at line 516 of file common.c.

517{
518 char buf[32];
519
520 SetVariable(pset.vars, "SHELL_ERROR",
521 (wait_result == 0) ? "false" : "true");
522 snprintf(buf, sizeof(buf), "%d", wait_result_to_exit_code(wait_result));
523 SetVariable(pset.vars, "SHELL_EXIT_CODE", buf);
524}
int wait_result_to_exit_code(int exit_status)
Definition: wait_error.c:138

References buf, pset, SetVariable(), snprintf, _psqlSettings::vars, and wait_result_to_exit_code().

Referenced by CloseGOutput(), do_copy(), do_shell(), evaluate_backtick(), exec_command_write(), and setQFout().

◆ SetupGOutput()

static bool SetupGOutput ( FILE **  gfile_fout,
bool *  is_pipe 
)
static

Definition at line 90 of file common.c.

91{
92 /* If there is a \g file or program, and it's not already open, open it */
93 if (pset.gfname != NULL && *gfile_fout == NULL)
94 {
95 if (openQueryOutputFile(pset.gfname, gfile_fout, is_pipe))
96 {
97 if (*is_pipe)
99 }
100 else
101 return false;
102 }
103 return true;
104}
void disable_sigpipe_trap(void)
Definition: print.c:3039

References disable_sigpipe_trap(), _psqlSettings::gfname, openQueryOutputFile(), and pset.

Referenced by ExecQueryAndProcessResults().

◆ skip_white_space()

static const char * skip_white_space ( const char *  query)
static

Definition at line 2201 of file common.c.

2202{
2203 int cnestlevel = 0; /* slash-star comment nest level */
2204
2205 while (*query)
2206 {
2207 int mblen = PQmblenBounded(query, pset.encoding);
2208
2209 /*
2210 * Note: we assume the encoding is a superset of ASCII, so that for
2211 * example "query[0] == '/'" is meaningful. However, we do NOT assume
2212 * that the second and subsequent bytes of a multibyte character
2213 * couldn't look like ASCII characters; so it is critical to advance
2214 * by mblen, not 1, whenever we haven't exactly identified the
2215 * character we are skipping over.
2216 */
2217 if (isspace((unsigned char) *query))
2218 query += mblen;
2219 else if (query[0] == '/' && query[1] == '*')
2220 {
2221 cnestlevel++;
2222 query += 2;
2223 }
2224 else if (cnestlevel > 0 && query[0] == '*' && query[1] == '/')
2225 {
2226 cnestlevel--;
2227 query += 2;
2228 }
2229 else if (cnestlevel == 0 && query[0] == '-' && query[1] == '-')
2230 {
2231 query += 2;
2232
2233 /*
2234 * We have to skip to end of line since any slash-star inside the
2235 * -- comment does NOT start a slash-star comment.
2236 */
2237 while (*query)
2238 {
2239 if (*query == '\n')
2240 {
2241 query++;
2242 break;
2243 }
2244 query += PQmblenBounded(query, pset.encoding);
2245 }
2246 }
2247 else if (cnestlevel > 0)
2248 query += mblen;
2249 else
2250 break; /* found first token */
2251 }
2252
2253 return query;
2254}

References _psqlSettings::encoding, PQmblenBounded(), and pset.

Referenced by command_no_begin().

◆ standard_strings()

bool standard_strings ( void  )

Definition at line 2500 of file common.c.

2501{
2502 const char *val;
2503
2504 if (!pset.db)
2505 return false;
2506
2507 val = PQparameterStatus(pset.db, "standard_conforming_strings");
2508
2509 if (val && strcmp(val, "on") == 0)
2510 return true;
2511
2512 return false;
2513}

References _psqlSettings::db, PQparameterStatus(), pset, and val.

Referenced by get_create_object_cmd(), main(), MainLoop(), and parse_slash_copy().

◆ StoreQueryTuple()

static bool StoreQueryTuple ( const PGresult result)
static

Definition at line 797 of file common.c.

798{
799 bool success = true;
800
801 if (PQntuples(result) < 1)
802 {
803 pg_log_error("no rows returned for \\gset");
804 success = false;
805 }
806 else if (PQntuples(result) > 1)
807 {
808 pg_log_error("more than one row returned for \\gset");
809 success = false;
810 }
811 else
812 {
813 int i;
814
815 for (i = 0; i < PQnfields(result); i++)
816 {
817 char *colname = PQfname(result, i);
818 char *varname;
819 char *value;
820
821 /* concatenate prefix and column name */
822 varname = psprintf("%s%s", pset.gset_prefix, colname);
823
824 if (VariableHasHook(pset.vars, varname))
825 {
826 pg_log_warning("attempt to \\gset into specially treated variable \"%s\" ignored",
827 varname);
828 continue;
829 }
830
831 if (!PQgetisnull(result, 0, i))
832 value = PQgetvalue(result, 0, i);
833 else
834 {
835 /* for NULL value, unset rather than set the variable */
836 value = NULL;
837 }
838
839 if (!SetVariable(pset.vars, varname, value))
840 {
841 free(varname);
842 success = false;
843 break;
844 }
845
846 free(varname);
847 }
848 }
849
850 return success;
851}
bool VariableHasHook(VariableSpace space, const char *name)
Definition: variables.c:437

References free, _psqlSettings::gset_prefix, i, pg_log_error, pg_log_warning, PQfname, PQgetisnull, PQgetvalue, PQnfields, PQntuples, pset, psprintf(), SetVariable(), success, value, VariableHasHook(), and _psqlSettings::vars.

Referenced by PrintQueryResult().

◆ uri_prefix_length()

static int uri_prefix_length ( const char *  connstr)
static

Definition at line 2634 of file common.c.

2635{
2636 /* The connection URI must start with either of the following designators: */
2637 static const char uri_designator[] = "postgresql://";
2638 static const char short_uri_designator[] = "postgres://";
2639
2640 if (strncmp(connstr, uri_designator,
2641 sizeof(uri_designator) - 1) == 0)
2642 return sizeof(uri_designator) - 1;
2643
2644 if (strncmp(connstr, short_uri_designator,
2645 sizeof(short_uri_designator) - 1) == 0)
2646 return sizeof(short_uri_designator) - 1;
2647
2648 return 0;
2649}
static const char short_uri_designator[]
Definition: fe-connect.c:450
static const char uri_designator[]
Definition: fe-connect.c:449

References connstr, short_uri_designator, and uri_designator.

Referenced by recognized_connection_string().

Variable Documentation

◆ sigint_interrupt_enabled

volatile sig_atomic_t sigint_interrupt_enabled = false

◆ sigint_interrupt_jmp

sigjmp_buf sigint_interrupt_jmp