PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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)
 
charpsql_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 charskip_white_space (const char *query)
 
bool is_superuser (void)
 
bool standard_strings (void)
 
const charsession_username (void)
 
charget_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 418 of file common.c.

419{
420 bool OK;
421
422 if (!result)
423 OK = false;
424 else
425 switch (PQresultStatus(result))
426 {
427 case PGRES_COMMAND_OK:
428 case PGRES_TUPLES_OK:
431 case PGRES_COPY_IN:
432 case PGRES_COPY_OUT:
434 /* Fine, do nothing */
435 OK = true;
436 break;
437
442 OK = false;
443 break;
444
445 default:
446 OK = false;
447 pg_log_error("unexpected PQresultStatus: %d",
449 break;
450 }
451
452 if (!OK && show_error)
453 {
454 const char *error = PQerrorMessage(pset.db);
455
456 if (strlen(error))
457 pg_log_info("%s", error);
458
460 }
461
462 return OK;
463}
static bool CheckConnection(void)
Definition common.c:356
uint32 result
char * PQerrorMessage(const PGconn *conn)
#define PQresultStatus
@ PGRES_COPY_IN
Definition libpq-fe.h:138
@ PGRES_COMMAND_OK
Definition libpq-fe.h:131
@ PGRES_TUPLES_CHUNK
Definition libpq-fe.h:148
@ PGRES_FATAL_ERROR
Definition libpq-fe.h:142
@ PGRES_COPY_OUT
Definition libpq-fe.h:137
@ PGRES_EMPTY_QUERY
Definition libpq-fe.h:130
@ PGRES_PIPELINE_SYNC
Definition libpq-fe.h:145
@ PGRES_BAD_RESPONSE
Definition libpq-fe.h:139
@ PGRES_PIPELINE_ABORTED
Definition libpq-fe.h:146
@ PGRES_NONFATAL_ERROR
Definition libpq-fe.h:141
@ PGRES_TUPLES_OK
Definition libpq-fe.h:134
#define pg_log_error(...)
Definition logging.h:108
#define pg_log_info(...)
Definition logging.h:126
static int fb(int x)
PsqlSettings pset
Definition startup.c:33
static void error(void)
PGconn * db
Definition settings.h:103

References CheckConnection(), _psqlSettings::db, error(), fb(), 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, pset, and result.

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

◆ CheckConnection()

static bool CheckConnection ( void  )
static

Definition at line 356 of file common.c.

357{
358 bool OK;
359
360 OK = ConnectionUp();
361 if (!OK)
362 {
364 {
365 pg_log_error("connection to server was lost");
367 }
368
369 fprintf(stderr, _("The connection to the server was lost. Attempting reset: "));
370 PQreset(pset.db);
372 OK = ConnectionUp();
373 if (!OK)
374 {
375 fprintf(stderr, _("Failed.\n"));
376
377 /*
378 * Transition to having no connection; but stash away the failed
379 * connection so that we can still refer to its parameters in a
380 * later \connect attempt. Keep the state cleanup here in sync
381 * with do_connect().
382 */
383 if (pset.dead_conn)
386 pset.db = NULL;
389 }
390 else
391 {
392 fprintf(stderr, _("Succeeded.\n"));
393
394 /*
395 * Re-sync, just in case anything changed. Keep this in sync with
396 * do_connect().
397 */
399 connection_warnings(false); /* Must be after SyncVariables */
400 }
401 }
402
403 return OK;
404}
static void pipelineReset(void)
Definition common.c:128
static bool ConnectionUp(void)
Definition common.c:337
void ResetCancelConn(void)
Definition cancel.c:107
void UnsyncVariables(void)
Definition command.c:4626
void SyncVariables(void)
Definition command.c:4571
void connection_warnings(bool in_startup)
Definition command.c:4443
#define fprintf(file, fmt, msg)
Definition cubescan.l:21
#define _(x)
Definition elog.c:96
void PQreset(PGconn *conn)
void PQfinish(PGconn *conn)
#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, fb(), 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 2703 of file common.c.

2704{
2705 int i;
2706
2707 switch (pset.send_mode)
2708 {
2709 case PSQL_SEND_EXTENDED_CLOSE: /* \close_prepared */
2711 break;
2712 case PSQL_SEND_EXTENDED_PARSE: /* \parse */
2714 break;
2715 case PSQL_SEND_EXTENDED_QUERY_PARAMS: /* \bind */
2716 case PSQL_SEND_EXTENDED_QUERY_PREPARED: /* \bind_named */
2717 for (i = 0; i < pset.bind_nparams; i++)
2722 break;
2723 case PSQL_SEND_QUERY:
2724 case PSQL_SEND_START_PIPELINE_MODE: /* \startpipeline */
2725 case PSQL_SEND_END_PIPELINE_MODE: /* \endpipeline */
2726 case PSQL_SEND_PIPELINE_SYNC: /* \syncpipeline */
2727 case PSQL_SEND_FLUSH: /* \flush */
2728 case PSQL_SEND_GET_RESULTS: /* \getresults */
2729 case PSQL_SEND_FLUSH_REQUEST: /* \flushrequest */
2730 break;
2731 }
2732
2733 pset.stmtName = NULL;
2735}
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
#define free(a)
char * stmtName
Definition settings.h:124
char ** bind_params
Definition settings.h:123
PSQL_SEND_MODE send_mode
Definition settings.h:120

References _psqlSettings::bind_nparams, _psqlSettings::bind_params, fb(), 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 584 of file common.c.

585{
587
588 while ((result = PQgetResult(pset.db)) != NULL)
590}
static void ClearOrSaveResult(PGresult *result)
Definition common.c:560
#define PQgetResult

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

Referenced by ExecQueryAndProcessResults().

◆ ClearOrSaveResult()

static void ClearOrSaveResult ( PGresult result)
static

Definition at line 560 of file common.c.

561{
562 if (result)
563 {
564 switch (PQresultStatus(result))
565 {
570 break;
571
572 default:
574 break;
575 }
576 }
577}
#define PQclear
PGresult * last_error_result
Definition settings.h:110

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

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 {
118 }
119 else
121 }
122}
void SetShellResultVariables(int wait_result)
Definition common.c:518
void restore_sigpipe_trap(void)
Definition print.c:3048

References fb(), restore_sigpipe_trap(), and SetShellResultVariables().

Referenced by ExecQueryAndProcessResults().

◆ command_no_begin()

static bool command_no_begin ( const char query)
static

Definition at line 2307 of file common.c.

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

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

Referenced by SendQuery().

◆ ConnectionUp()

static bool ConnectionUp ( void  )
static

Definition at line 337 of file common.c.

338{
339 return PQstatus(pset.db) != CONNECTION_BAD;
340}
ConnStatusType PQstatus(const PGconn *conn)
@ CONNECTION_BAD
Definition libpq-fe.h:91

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

Referenced by CheckConnection(), discardAbortedPipelineResults(), ExecQueryAndProcessResults(), and SendQuery().

◆ DescribeQuery()

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

Definition at line 1352 of file common.c.

1353{
1354 bool timing = pset.timing;
1356 bool OK;
1358 after;
1359
1360 *elapsed_msec = 0;
1361
1362 if (timing)
1364 else
1366
1367 /*
1368 * To parse the query but not execute it, we prepare it, using the unnamed
1369 * prepared statement. This is invisible to psql users, since there's no
1370 * way to access the unnamed prepared statement from psql user space. The
1371 * next Parse or Query protocol message would overwrite the statement
1372 * anyway. (So there's no great need to clear it when done, which is a
1373 * good thing because libpq provides no easy way to do that.)
1374 */
1375 result = PQprepare(pset.db, "", query, 0, NULL);
1377 {
1379 SetResultVariables(result, false);
1381 return false;
1382 }
1383 PQclear(result);
1384
1386 OK = AcceptResult(result, true) &&
1388 if (OK && result)
1389 {
1390 if (PQnfields(result) > 0)
1391 {
1393 int i;
1394
1396
1398 "SELECT name AS \"%s\", pg_catalog.format_type(tp, tpm) AS \"%s\"\n"
1399 "FROM (VALUES ",
1400 gettext_noop("Column"),
1401 gettext_noop("Type"));
1402
1403 for (i = 0; i < PQnfields(result); i++)
1404 {
1405 const char *name;
1406 char *escname;
1407
1408 if (i > 0)
1410
1411 name = PQfname(result, i);
1413
1414 if (escname == NULL)
1415 {
1417 PQclear(result);
1419 return false;
1420 }
1421
1422 appendPQExpBuffer(&buf, "(%s, '%u'::pg_catalog.oid, %d)",
1423 escname,
1424 PQftype(result, i),
1425 PQfmod(result, i));
1426
1428 }
1429
1430 appendPQExpBufferStr(&buf, ") s(name, tp, tpm)");
1431 PQclear(result);
1432
1433 result = PQexec(pset.db, buf.data);
1434 OK = AcceptResult(result, true);
1435
1436 if (timing)
1437 {
1441 }
1442
1443 if (OK && result)
1444 OK = PrintQueryResult(result, true, NULL, NULL, NULL);
1445
1447 }
1448 else
1450 _("The command has no result, or the result has no columns.\n"));
1451 }
1452
1455
1456 return OK;
1457}
static bool PrintQueryResult(PGresult *result, bool last, const printQueryOpt *opt, FILE *printQueryFout, FILE *printStatusFout)
Definition common.c:1041
static void SetResultVariables(PGresult *result, bool success)
Definition common.c:478
static bool AcceptResult(const PGresult *result, bool show_error)
Definition common.c:418
#define gettext_noop(x)
Definition c.h:1285
void PQfreemem(void *ptr)
Definition fe-exec.c:4068
Oid PQftype(const PGresult *res, int field_num)
Definition fe-exec.c:3750
PGresult * PQdescribePrepared(PGconn *conn, const char *stmt)
Definition fe-exec.c:2472
PGresult * PQprepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes)
Definition fe-exec.c:2323
char * PQescapeLiteral(PGconn *conn, const char *str, size_t len)
Definition fe-exec.c:4418
int PQfmod(const PGresult *res, int field_num)
Definition fe-exec.c:3772
PGresult * PQexec(PGconn *conn, const char *query)
Definition fe-exec.c:2279
#define INSTR_TIME_SET_CURRENT(t)
Definition instr_time.h:434
#define INSTR_TIME_SUBTRACT(x, y)
Definition instr_time.h:444
#define INSTR_TIME_GET_MILLISEC(t)
Definition instr_time.h:459
#define INSTR_TIME_SET_ZERO(t)
Definition instr_time.h:429
#define PQnfields
#define PQfname
static char buf[DEFAULT_XLOG_SEG_SIZE]
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
void initPQExpBuffer(PQExpBuffer str)
Definition pqexpbuffer.c:90
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
void appendPQExpBufferChar(PQExpBuffer str, char ch)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
void termPQExpBuffer(PQExpBuffer str)
static int before(chr x, chr y)
FILE * queryFout
Definition settings.h:105
const char * name

References _, AcceptResult(), appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), before(), buf, ClearOrSaveResult(), _psqlSettings::db, fb(), 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, result, SetResultVariables(), termPQExpBuffer(), and _psqlSettings::timing.

Referenced by SendQuery().

◆ discardAbortedPipelineResults()

static PGresult * discardAbortedPipelineResults ( void  )
static

Definition at line 1468 of file common.c.

1469{
1470 for (;;)
1471 {
1472 PGresult *res = PQgetResult(pset.db);
1474
1476 {
1477 /*
1478 * Found a synchronisation point. The sync counter is decremented
1479 * by the caller.
1480 */
1481 return res;
1482 }
1483 else if (res != NULL && result_status == PGRES_FATAL_ERROR)
1484 {
1485 /*
1486 * Found a FATAL error sent by the backend, and we cannot recover
1487 * from this state. Instead, return the last result and let the
1488 * outer loop handle it.
1489 */
1491
1492 /*
1493 * Fetch result to consume the end of the current query being
1494 * processed.
1495 */
1497 Assert(fatal_res == NULL);
1498 return res;
1499 }
1500 else if (res == NULL)
1501 {
1502 /*
1503 * A query was processed, decrement the counters.
1504 *
1505 * It is possible to get here with available_results == 0 when an
1506 * error is generated by the Sync message processing itself. Such
1507 * errors are not counted in available_results because they are
1508 * not associated with a piped command. In that case, skip the
1509 * counter decrements and continue to find the Sync result.
1510 *
1511 * If the connection has been lost, there will never be any more
1512 * results to read, so bail out.
1513 */
1514 if (!ConnectionUp())
1515 return NULL;
1516 if (pset.available_results > 0)
1518 if (pset.requested_results > 0)
1520 }
1521
1522 if (pset.requested_results == 0)
1523 {
1524 /* We have read all the requested results, leave */
1525 return res;
1526 }
1527
1528 if (pset.available_results == 0 && pset.piped_syncs == 0)
1529 {
1530 /*
1531 * There are no more results to get and there is no
1532 * synchronisation point to stop at. This will leave the pipeline
1533 * in an aborted state.
1534 */
1535 return res;
1536 }
1537
1538 /*
1539 * An aborted pipeline will have either NULL results or results in an
1540 * PGRES_PIPELINE_ABORTED status.
1541 */
1543 PQclear(res);
1544 }
1545}
#define PG_USED_FOR_ASSERTS_ONLY
Definition c.h:249
#define Assert(condition)
Definition c.h:943
ExecStatusType
Definition libpq-fe.h:129
int available_results
Definition settings.h:128
int requested_results
Definition settings.h:129

References Assert, _psqlSettings::available_results, ConnectionUp(), _psqlSettings::db, fb(), 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 1571 of file common.c.

1575{
1576 bool timing = pset.timing;
1577 bool success = false;
1578 bool return_early = false;
1579 bool end_pipeline = false;
1581 after;
1583 FILE *gfile_fout = NULL;
1584 bool gfile_is_pipe = false;
1585
1586 if (timing)
1588 else
1590
1591 switch (pset.send_mode)
1592 {
1597 break;
1599 success = PQsendPrepare(pset.db, pset.stmtName, query, 0, NULL);
1602 break;
1607 (const char *const *) pset.bind_params,
1608 NULL, NULL, 0);
1611 break;
1616 (const char *const *) pset.bind_params,
1617 NULL, NULL, 0);
1620 break;
1623 break;
1627 {
1628 /*
1629 * End of the pipeline, all queued commands need to be
1630 * processed.
1631 */
1632 end_pipeline = true;
1633 pset.piped_syncs++;
1634
1635 /*
1636 * The server will send a ReadyForQuery after a Sync is
1637 * processed, flushing all the results back to the client.
1638 */
1640 pset.piped_commands = 0;
1641
1642 /* We want to read all results */
1644 }
1645 break;
1649 {
1650 pset.piped_syncs++;
1651
1652 /*
1653 * The server will send a ReadyForQuery after a Sync is
1654 * processed, flushing all the results back to the client.
1655 */
1657 pset.piped_commands = 0;
1658 }
1659 break;
1660 case PSQL_SEND_FLUSH:
1662 break;
1666 {
1667 /*
1668 * With the flush request, all commands in the pipeline are
1669 * pushed and the server will flush the results back to the
1670 * client, making them available.
1671 */
1673 pset.piped_commands = 0;
1674 }
1675 break;
1677 if (pset.available_results == 0 && pset.piped_syncs == 0)
1678 {
1679 /*
1680 * If no sync or flush request were sent, PQgetResult() would
1681 * block as there are no results available. Forbid any
1682 * attempt to get pending results should we try to reach this
1683 * state.
1684 */
1685 pg_log_info("No pending results to get");
1686 success = false;
1688 }
1689 else
1690 {
1691 success = true;
1692
1693 /*
1694 * Cap requested_results to the maximum number of known
1695 * results.
1696 */
1697 if (pset.requested_results == 0 ||
1700 }
1701 break;
1702 case PSQL_SEND_QUERY:
1704 {
1706 0, NULL, NULL, NULL, NULL, 0);
1707 if (success)
1709 }
1710 else
1711 success = PQsendQuery(pset.db, query);
1712 break;
1713 }
1714
1715 if (!success)
1716 {
1717 const char *error = PQerrorMessage(pset.db);
1718
1719 if (strlen(error))
1720 pg_log_info("%s", error);
1721
1723
1725
1726 return -1;
1727 }
1728
1729 if (pset.requested_results == 0 && !end_pipeline &&
1731 {
1732 /*
1733 * We are in a pipeline and have not reached the pipeline end, or
1734 * there was no request to read pipeline results. Update the psql
1735 * variables tracking the pipeline activity and exit.
1736 */
1738 return 1;
1739 }
1740
1741 /*
1742 * Fetch the result in chunks if FETCH_COUNT is set, except when:
1743 *
1744 * * SHOW_ALL_RESULTS is false, since that requires us to complete the
1745 * query before we can tell if its results should be displayed.
1746 *
1747 * * We're doing \crosstab, which likewise needs to see all the rows at
1748 * once.
1749 *
1750 * * We're doing \gexec: we must complete the data fetch to make the
1751 * connection free for issuing the resulting commands.
1752 *
1753 * * We're doing \gset: only one result row is allowed anyway.
1754 *
1755 * * We're doing \watch: users probably don't want us to force use of the
1756 * pager for that, plus chunking could break the min_rows check.
1757 */
1761 {
1763 pg_log_warning("fetching results in chunked mode failed");
1764 }
1765
1766 /*
1767 * If SIGINT is sent while the query is processing, the interrupt will be
1768 * consumed. The user's intention, though, is to cancel the entire watch
1769 * process, so detect a sent cancellation request and exit in this case.
1770 */
1771 if (is_watch && cancel_pressed)
1772 {
1774 return 0;
1775 }
1776
1777 /* first result */
1779 if (min_rows > 0 && PQntuples(result) < min_rows)
1780 {
1781 return_early = true;
1782 }
1783
1784 while (result != NULL)
1785 {
1787 bool is_chunked_result = false;
1789 bool last;
1790
1791 if (!AcceptResult(result, false))
1792 {
1793 /*
1794 * Some error occurred, either a server-side failure or a failure
1795 * to submit the command string. Record that.
1796 */
1797 const char *error = PQresultErrorMessage(result);
1798
1799 if (strlen(error))
1800 pg_log_info("%s", error);
1801
1803 if (!is_watch)
1804 SetResultVariables(result, false);
1805
1806 /* keep the result status before clearing it */
1809 success = false;
1810
1812 pg_log_info("Pipeline aborted, command did not run");
1813
1814 /*
1815 * switch to next result
1816 */
1820 {
1821 /*
1822 * For some obscure reason PQgetResult does *not* return a
1823 * NULL in copy cases despite the result having been cleared,
1824 * but keeps returning an "empty" result that we have to
1825 * ignore manually.
1826 */
1827 result = NULL;
1828 }
1829 else if ((end_pipeline || pset.requested_results > 0)
1831 {
1832 /*
1833 * Error within a pipeline. All commands are aborted until
1834 * the next synchronisation point. We need to consume all the
1835 * results until this synchronisation point, or stop when
1836 * there are no more result to discard.
1837 *
1838 * Checking the pipeline status is necessary for the case
1839 * where the connection was reset. The new connection is not
1840 * in any kind of pipeline state and thus has no result to
1841 * discard.
1842 */
1844 }
1845 else
1847
1848 /*
1849 * Get current timing measure in case an error occurs
1850 */
1851 if (timing)
1852 {
1856 }
1857
1858 continue;
1859 }
1860 else if (svpt_gone_p && !*svpt_gone_p)
1861 {
1862 /*
1863 * Check if the user ran any command that would destroy our
1864 * internal savepoint: If the user did COMMIT AND CHAIN, RELEASE
1865 * or ROLLBACK, our savepoint is gone. If they issued a SAVEPOINT,
1866 * releasing ours would remove theirs.
1867 */
1868 const char *cmd = PQcmdStatus(result);
1869
1870 *svpt_gone_p = (strcmp(cmd, "COMMIT") == 0 ||
1871 strcmp(cmd, "SAVEPOINT") == 0 ||
1872 strcmp(cmd, "RELEASE") == 0 ||
1873 strcmp(cmd, "ROLLBACK") == 0);
1874 }
1875
1877
1878 /* must handle COPY before changing the current result */
1882 {
1884
1886 {
1887 /*
1888 * Running COPY within a pipeline can break the protocol
1889 * synchronisation in multiple ways, and psql shows its limits
1890 * when it comes to tracking this information.
1891 *
1892 * While in COPY mode, the backend process ignores additional
1893 * Sync messages and will not send the matching ReadyForQuery
1894 * expected by the frontend.
1895 *
1896 * Additionally, libpq automatically sends a Sync with the
1897 * Copy message, creating an unexpected synchronisation point.
1898 * A failure during COPY would leave the pipeline in an
1899 * aborted state while the backend would be in a clean state,
1900 * ready to process commands.
1901 *
1902 * Improving those issues would require modifications in how
1903 * libpq handles pipelines and COPY. Hence, for the time
1904 * being, we forbid the use of COPY within a pipeline,
1905 * aborting the connection to avoid an inconsistent state on
1906 * psql side if trying to use a COPY command.
1907 */
1908 pg_log_info("COPY in a pipeline is not supported, aborting connection");
1910 }
1911
1912 /*
1913 * For COPY OUT, direct the output to the default place (probably
1914 * a pager pipe) for \watch, or to pset.copyStream for \copy,
1915 * otherwise to pset.gfname if that's set, otherwise to
1916 * pset.queryFout.
1917 */
1919 {
1920 if (is_watch)
1921 {
1922 /* invoked by \watch */
1924 }
1925 else if (pset.copyStream)
1926 {
1927 /* invoked by \copy */
1929 }
1930 else if (pset.gfname)
1931 {
1932 /* COPY followed by \g filename or \g |program */
1934 if (gfile_fout)
1936 }
1937 else
1938 {
1939 /* fall back to the generic query output stream */
1941 }
1942 }
1943
1944 /*
1945 * Even if the output stream could not be opened, we call
1946 * HandleCopyResult() with a NULL output stream to collect and
1947 * discard the COPY data.
1948 */
1950 }
1951
1952 /* If we have a chunked result, collect and print all chunks */
1954 {
1956 printQueryOpt my_popt = opt ? *opt : pset.popt;
1957 int64 total_tuples = 0;
1958 bool is_pager = false;
1959 int flush_error = 0;
1960
1961 /* initialize print options for partial table output */
1962 my_popt.topt.start_table = true;
1963 my_popt.topt.stop_table = false;
1964 my_popt.topt.prior_records = 0;
1965
1966 /* open \g file if needed */
1968 if (gfile_fout)
1970
1971 /* force use of pager for any chunked resultset going to stdout */
1972 if (success && tuples_fout == stdout)
1973 {
1975 is_pager = true;
1976 }
1977
1978 do
1979 {
1980 /*
1981 * Display the current chunk of results, unless the output
1982 * stream stopped working or we got canceled. We skip use of
1983 * PrintQueryResult and go directly to printQuery, so that we
1984 * can pass the correct is_pager value and because we don't
1985 * want PrintQueryStatus to happen yet. Above, we rejected
1986 * use of chunking for all cases in which PrintQueryResult
1987 * would send the result to someplace other than printQuery.
1988 */
1990 {
1993 }
1994
1995 /* after the first result set, disallow header decoration */
1996 my_popt.topt.start_table = false;
1997
1998 /* count tuples before dropping the result */
1999 my_popt.topt.prior_records += PQntuples(result);
2000 total_tuples += PQntuples(result);
2001
2003
2004 /* get the next result, loop if it's PGRES_TUPLES_CHUNK */
2007
2008 /* We expect an empty PGRES_TUPLES_OK, else there's a problem */
2010 {
2011 char buf[32];
2012
2013 Assert(PQntuples(result) == 0);
2014
2015 /* Display the footer using the empty result */
2017 {
2018 my_popt.topt.stop_table = true;
2021 }
2022
2023 if (is_pager)
2025
2026 /*
2027 * It's possible the data is from a RETURNING clause, in which
2028 * case we need to print query status.
2029 */
2031
2032 /*
2033 * We must do a fake SetResultVariables(), since we don't have
2034 * a PGresult corresponding to the whole query.
2035 */
2036 SetVariable(pset.vars, "ERROR", "false");
2037 SetVariable(pset.vars, "SQLSTATE", "00000");
2038 snprintf(buf, sizeof(buf), INT64_FORMAT, total_tuples);
2039 SetVariable(pset.vars, "ROW_COUNT", buf);
2040 /* Prevent SetResultVariables call below */
2041 is_chunked_result = true;
2042
2043 /* Clear the empty result so it isn't printed below */
2045 result = NULL;
2046 }
2047 else
2048 {
2049 /* Probably an error report, so close the pager and print it */
2050 if (is_pager)
2052
2053 success &= AcceptResult(result, true);
2054 /* SetResultVariables and ClearOrSaveResult happen below */
2055 }
2056 }
2057
2059 {
2060 /*
2061 * Sync response, decrease the sync and requested_results
2062 * counters. Guard against underflow: an error during Sync
2063 * processing on the server can cause the client-side counter to
2064 * drift.
2065 */
2066 if (pset.piped_syncs > 0)
2067 pset.piped_syncs--;
2068 if (pset.requested_results > 0)
2070
2071 /*
2072 * After a synchronisation point, reset success state to print
2073 * possible successful results that will be processed after this.
2074 */
2075 success = true;
2076
2077 /*
2078 * If all syncs were processed and pipeline end was requested,
2079 * exit pipeline mode.
2080 */
2081 if (end_pipeline && pset.piped_syncs == 0)
2083 }
2084 else if (PQpipelineStatus(pset.db) != PQ_PIPELINE_OFF &&
2086 {
2087 /*
2088 * In a pipeline with a non-sync response? Decrease the result
2089 * counters.
2090 */
2091 if (pset.available_results > 0)
2093 if (pset.requested_results > 0)
2095 }
2096
2097 /*
2098 * Check PQgetResult() again. In the typical case of a single-command
2099 * string, it will return NULL. Otherwise, we'll have other results
2100 * to process. We need to do that to check whether this is the last.
2101 */
2104 else
2105 {
2106 /*
2107 * In pipeline mode, a NULL result indicates the end of the
2108 * current query being processed. Call PQgetResult() once to
2109 * consume this state.
2110 */
2112 {
2115 }
2116
2117 /* Now, we can get the next result in the pipeline. */
2118 if (pset.requested_results > 0)
2120 }
2121
2122 last = (next_result == NULL);
2123
2124 /*
2125 * Update current timing measure.
2126 *
2127 * It will include the display of previous results, if any. This
2128 * cannot be helped because the server goes on processing further
2129 * queries anyway while the previous ones are being displayed. The
2130 * parallel execution of the client display hides the server time when
2131 * it is shorter.
2132 *
2133 * With combined queries, timing must be understood as an upper bound
2134 * of the time spent processing them.
2135 */
2136 if (timing)
2137 {
2141 }
2142
2143 /*
2144 * This may or may not print something depending on settings.
2145 *
2146 * A pipeline sync will have a non-NULL result but does not have
2147 * anything to print, thus ignore results in this case.
2148 */
2150 {
2151 /*
2152 * If results need to be printed into the file specified by \g,
2153 * open it, unless we already did. Note that when pset.gfname is
2154 * set, the passed-in value of printQueryFout is not used for
2155 * tuple output, but it's still used for status output.
2156 */
2158
2161 if (gfile_fout)
2163 if (success)
2164 success &= PrintQueryResult(result, last, opt,
2166 }
2167
2168 /* set variables from last result, unless dealt with elsewhere */
2169 if (last && !is_watch && !is_chunked_result)
2171
2174
2176 {
2177 /*
2178 * Outside of a pipeline, drop the next result, as well as any
2179 * others not yet read.
2180 *
2181 * Within a pipeline, we can let the outer loop handle this as an
2182 * aborted pipeline, which will discard then all the results.
2183 */
2186 break;
2187 }
2188 }
2189
2190 /* close \g file if we opened it */
2192
2193 if (end_pipeline)
2194 {
2195 /*
2196 * Reset available/requested results. Normally these are already 0,
2197 * but an error generated by a Sync processing itself can leave some
2198 * of them behind. Consume them before exiting pipeline mode.
2199 */
2200 while (pset.piped_syncs > 0)
2201 {
2203
2205
2206 if (remaining == NULL)
2207 {
2208 if (!ConnectionUp())
2209 break;
2210 continue;
2211 }
2213 pset.piped_syncs--;
2215 }
2216 pset.piped_syncs = 0;
2217 pset.piped_commands = 0;
2220
2223 }
2225
2227
2228 /* may need this to recover from conn loss during COPY */
2229 if (!CheckConnection())
2230 return -1;
2231
2233 return 0;
2234
2235 return success ? 1 : -1;
2236}
static void PrintQueryStatus(PGresult *result, FILE *printQueryFout)
Definition common.c:994
static void ClearOrSaveAllResults(void)
Definition common.c:584
static PGresult * discardAbortedPipelineResults(void)
Definition common.c:1468
static bool SetupGOutput(FILE **gfile_fout, bool *is_pipe)
Definition common.c:90
static bool HandleCopyResult(PGresult **resultp, FILE *copystream)
Definition common.c:939
static void CloseGOutput(FILE *gfile_fout, bool is_pipe)
Definition common.c:110
static void SetPipelineVariables(void)
Definition common.c:536
#define INT64_FORMAT
Definition c.h:634
int64_t int64
Definition c.h:621
PGpipelineStatus PQpipelineStatus(const PGconn *conn)
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:1509
int PQflush(PGconn *conn)
Definition fe-exec.c:4036
int PQexitPipelineMode(PGconn *conn)
Definition fe-exec.c:3104
int PQenterPipelineMode(PGconn *conn)
Definition fe-exec.c:3073
int PQsendClosePrepared(PGconn *conn, const char *stmt)
Definition fe-exec.c:2573
int PQsendPipelineSync(PGconn *conn)
Definition fe-exec.c:3313
int PQsendPrepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes)
Definition fe-exec.c:1553
int PQsetChunkedRowsMode(PGconn *conn, int chunkSize)
Definition fe-exec.c:1982
int PQsendQuery(PGconn *conn, const char *query)
Definition fe-exec.c:1433
int PQpipelineSync(PGconn *conn)
Definition fe-exec.c:3303
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:1650
int PQsendFlushRequest(PGconn *conn)
Definition fe-exec.c:3402
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition print.c:3743
FILE * PageOutput(int lines, const printTableOpt *topt)
Definition print.c:3079
void ClosePager(FILE *pagerpipe)
Definition print.c:3161
volatile sig_atomic_t cancel_pressed
Definition print.c:48
int remaining
Definition informix.c:692
static bool success
Definition initdb.c:188
#define PQresultErrorMessage
#define PQcmdStatus
#define PQntuples
@ PGRES_COPY_BOTH
Definition libpq-fe.h:143
@ PQ_PIPELINE_OFF
Definition libpq-fe.h:193
#define pg_log_warning(...)
Definition pgfnames.c:24
#define snprintf
Definition port.h:261
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
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
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition variables.c:282

References AcceptResult(), Assert, _psqlSettings::available_results, before(), _psqlSettings::bind_nparams, _psqlSettings::bind_params, buf, cancel_pressed, CheckConnection(), ClearOrSaveAllResults(), ClearOrSaveResult(), CloseGOutput(), ClosePager(), ConnectionUp(), _psqlSettings::copyStream, _psqlSettings::crosstab_flag, _psqlSettings::db, discardAbortedPipelineResults(), error(), EXIT_BADCONN, fb(), _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, PQclear, PQcmdStatus, PQenterPipelineMode(), PQerrorMessage(), PQexitPipelineMode(), PQflush(), PQgetResult, PQntuples, PQpipelineStatus(), PQpipelineSync(), PQresultErrorMessage, PQresultStatus, PQsendClosePrepared(), PQsendFlushRequest(), PQsendPipelineSync(), PQsendPrepare(), PQsendQuery(), PQsendQueryParams(), PQsendQueryPrepared(), PQsetChunkedRowsMode(), printQuery(), PrintQueryResult(), PrintQueryStatus(), 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, remaining, _psqlSettings::requested_results, result, _psqlSettings::send_mode, SetPipelineVariables(), SetResultVariables(), SetupGOutput(), SetVariable(), _psqlSettings::show_all_results, snprintf, printTableOpt::start_table, _psqlSettings::stmtName, success, _psqlSettings::timing, printQueryOpt::topt, and _psqlSettings::vars.

Referenced by PSQLexecWatch(), and SendQuery().

◆ ExecQueryTuples()

static bool ExecQueryTuples ( const PGresult result)
static

Definition at line 863 of file common.c.

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

Referenced by PrintQueryResult().

◆ expand_tilde()

void expand_tilde ( char **  filename)

Definition at line 2619 of file common.c.

2620{
2621 if (!filename || !(*filename))
2622 return;
2623
2624 /*
2625 * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde
2626 * for short versions of long file names, though the tilde is usually
2627 * toward the end, not at the beginning.
2628 */
2629#ifndef WIN32
2630
2631 /* try tilde expansion */
2632 if (**filename == '~')
2633 {
2634 char *fn;
2635 char oldp,
2636 *p;
2637 struct passwd *pw;
2638 char home[MAXPGPATH];
2639
2640 fn = *filename;
2641 *home = '\0';
2642
2643 p = fn + 1;
2644 while (*p != '/' && *p != '\0')
2645 p++;
2646
2647 oldp = *p;
2648 *p = '\0';
2649
2650 if (*(fn + 1) == '\0')
2651 get_home_path(home); /* ~ or ~/ only */
2652 else if ((pw = getpwnam(fn + 1)) != NULL)
2653 strlcpy(home, pw->pw_dir, sizeof(home)); /* ~user */
2654
2655 *p = oldp;
2656 if (strlen(home) != 0)
2657 {
2658 char *newfn;
2659
2660 newfn = psprintf("%s%s", home, p);
2661 free(fn);
2662 *filename = newfn;
2663 }
2664 }
2665#endif
2666}
#define MAXPGPATH
static char * filename
Definition pg_dumpall.c:133
bool get_home_path(char *ret_path)
Definition path.c:1022
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)

References fb(), 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 2582 of file common.c.

2583{
2586 char *res = NULL;
2587
2588 if (pset.db == NULL)
2589 return NULL;
2590
2592 if (opts == NULL)
2593 return NULL;
2594
2595 for (PQconninfoOption *opt = opts; opt->keyword != NULL; ++opt)
2596 {
2597 if (strcmp(opt->keyword, keyword) == 0)
2598 {
2599 serviceopt = opt;
2600 break;
2601 }
2602 }
2603
2604 /* Take a copy of the value, as it is freed by PQconninfoFree(). */
2605 if (serviceopt && serviceopt->val != NULL)
2606 res = pg_strdup(serviceopt->val);
2608
2609 return res;
2610}
PQconninfoOption * PQconninfo(PGconn *conn)
void PQconninfoFree(PQconninfoOption *connOptions)
char * pg_strdup(const char *in)
Definition fe_memutils.c:91
static AmcheckOptions opts
Definition pg_amcheck.c:112

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

Referenced by SyncVariables().

◆ HandleCopyResult()

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

Definition at line 939 of file common.c.

940{
941 bool success;
944
947
949
951 {
955 && (copystream != NULL);
956
957 /*
958 * Suppress status printing if the report would go to the same place
959 * as the COPY data just went. Note this doesn't prevent error
960 * reporting, since handleCopyOut did that.
961 */
963 {
966 }
967 }
968 else
969 {
970 /* COPY IN */
971 /* Ignore the copystream argument passed to the function */
976 &copy_result);
977 }
979
980 /*
981 * Replace the PGRES_COPY_OUT/IN result with COPY command's exit status,
982 * or with NULL if we want to suppress printing anything.
983 */
986
987 return success;
988}
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:3528
FILE * cur_cmd_source
Definition settings.h:138

References Assert, _psqlSettings::copyStream, _psqlSettings::cur_cmd_source, _psqlSettings::db, fb(), 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 2522 of file common.c.

2523{
2524 const char *val;
2525
2526 if (!pset.db)
2527 return false;
2528
2529 val = PQparameterStatus(pset.db, "is_superuser");
2530
2531 if (val && strcmp(val, "on") == 0)
2532 return true;
2533
2534 return false;
2535}
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
long val
Definition informix.c:689

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

Referenced by check_role(), check_session_authorization(), get_prompt(), getSubscriptions(), InitializeSessionUserId(), SetCurrentRoleId(), SetOuterUserId(), SetSessionAuthorization(), and SetSessionUserId().

◆ 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}
Datum arg
Definition elog.c:1323

References arg, fb(), 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}
static Archive * fout
Definition pg_dumpall.c:139

References fb(), fout, and pg_log_error.

Referenced by setQFout(), and SetupGOutput().

◆ pipelineReset()

◆ PrintNotifications()

static void PrintNotifications ( void  )
static

Definition at line 742 of file common.c.

743{
745
747 while ((notify = PQnotifies(pset.db)) != NULL)
748 {
749 /* for backward compatibility, only show payload if nonempty */
750 if (notify->extra[0])
751 fprintf(pset.queryFout, _("Asynchronous notification \"%s\" with payload \"%s\" received from server process with PID %d.\n"),
752 notify->relname, notify->extra, notify->be_pid);
753 else
754 fprintf(pset.queryFout, _("Asynchronous notification \"%s\" received from server process with PID %d.\n"),
755 notify->relname, notify->be_pid);
759 }
760}
int PQconsumeInput(PGconn *conn)
Definition fe-exec.c:2001
PGnotify * PQnotifies(PGconn *conn)
Definition fe-exec.c:2684

References _, _psqlSettings::db, fb(), fprintf, PQconsumeInput(), PQfreemem(), PQnotifies(), pset, and _psqlSettings::queryFout.

Referenced by SendQuery().

◆ PrintQueryResult()

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

Definition at line 1041 of file common.c.

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

References _psqlSettings::crosstab_flag, ExecQueryTuples(), fb(), _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, result, _psqlSettings::show_all_results, StoreQueryTuple(), and success.

Referenced by DescribeQuery(), and ExecQueryAndProcessResults().

◆ PrintQueryStatus()

static void PrintQueryStatus ( PGresult result,
FILE printQueryFout 
)
static

Definition at line 994 of file common.c.

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

References buf, fb(), printTableOpt::format, fout, fprintf, html_escaped_print(), _psqlSettings::logfile, PGRES_TUPLES_OK, _psqlSettings::popt, PQcmdStatus, PQoidValue(), PQresultStatus, PRINT_HTML, pset, _psqlSettings::queryFout, _psqlSettings::quiet, result, 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 775 of file common.c.

777{
778 bool ok = true;
780
781 printQuery(result, opt ? opt : &pset.popt, fout, false, pset.logfile);
782 fflush(fout);
783 if (ferror(fout))
784 {
785 pg_log_error("could not print result table: %m");
786 ok = false;
787 }
788
789 return ok;
790}
#define false
static FILE * logfile
Definition pg_regress.c:128

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

Referenced by PrintQueryResult().

◆ PrintTiming()

static void PrintTiming ( double  elapsed_msec)
static

Definition at line 598 of file common.c.

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

References _, days, fb(), 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 {
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, fb(), 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 */
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:
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)
225 else
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 */
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)
char * PQescapeIdentifier(PGconn *conn, const char *str, size_t len)
Definition fe-exec.c:4424
static struct @177 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)
const char * GetVariable(VariableSpace space, const char *name)
Definition variables.c:73

References appendShellStringNoError(), buf, conditional_active(), _psqlSettings::db, error(), fb(), 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, result, 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 657 of file common.c.

658{
659 PGresult *res;
660
661 if (!pset.db)
662 {
663 pg_log_error("You are currently not connected to a database.");
664 return NULL;
665 }
666
668 {
669 printf(_("/**** INTERNAL QUERY ****/\n"
670 "%s\n"
671 "/************************/\n\n"), query);
672 fflush(stdout);
673 if (pset.logfile)
674 {
676 _("/**** INTERNAL QUERY ****/\n"
677 "%s\n"
678 "/************************/\n\n"), query);
680 }
681
683 return NULL;
684 }
685
687
688 res = PQexec(pset.db, query);
689
691
692 if (!AcceptResult(res, true))
693 {
695 res = NULL;
696 }
697
698 return res;
699}
@ 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, fb(), fprintf, _psqlSettings::logfile, pg_log_error, PQexec(), printf, pset, PSQL_ECHO_HIDDEN_NOEXEC, PSQL_ECHO_HIDDEN_OFF, ResetCancelConn(), and SetCancelConn().

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 712 of file common.c.

713{
714 bool timing = pset.timing;
715 double elapsed_msec = 0;
716 int res;
717
718 if (!pset.db)
719 {
720 pg_log_error("You are currently not connected to a database.");
721 return 0;
722 }
723
725
727
729
730 /* Possible microtiming output */
731 if (timing)
733
734 return res;
735}
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:1571
static void PrintTiming(double elapsed_msec)
Definition common.c:598

References _psqlSettings::db, ExecQueryAndProcessResults(), fb(), 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 2747 of file common.c.

2748{
2749 return uri_prefix_length(connstr) != 0 || strchr(connstr, '=') != NULL;
2750}
static int uri_prefix_length(const char *connstr)
Definition common.c:2677
static const char * connstr
Definition pg_dumpall.c:95

References connstr, fb(), and uri_prefix_length().

Referenced by do_connect().

◆ SendQuery()

bool SendQuery ( const char query)

Definition at line 1120 of file common.c.

1121{
1122 bool timing = pset.timing;
1124 double elapsed_msec = 0;
1125 bool OK = false;
1126 int i;
1127 bool on_error_rollback_savepoint = false;
1128 bool svpt_gone = false;
1129
1130 if (!pset.db)
1131 {
1132 pg_log_error("You are currently not connected to a database.");
1133 goto sendquery_cleanup;
1134 }
1135
1136 if (pset.singlestep)
1137 {
1138 char buf[3];
1139
1140 fflush(stderr);
1141 printf(_("/**(Single step mode: verify command)******************************************/\n"
1142 "%s\n"
1143 "/**(press return to proceed or enter x and return to cancel)*******************/\n"),
1144 query);
1145 fflush(stdout);
1146 if (fgets(buf, sizeof(buf), stdin) != NULL)
1147 if (buf[0] == 'x')
1148 goto sendquery_cleanup;
1149 if (cancel_pressed)
1150 goto sendquery_cleanup;
1151 }
1152 else if (pset.echo == PSQL_ECHO_QUERIES)
1153 {
1154 puts(query);
1155 fflush(stdout);
1156 }
1157
1158 if (pset.logfile)
1159 {
1161 _("/******** QUERY *********/\n"
1162 "%s\n"
1163 "/************************/\n\n"), query);
1165 }
1166
1168
1170
1172 !pset.autocommit &&
1173 !command_no_begin(query))
1174 {
1176
1177 result = PQexec(pset.db, "BEGIN");
1179 {
1182 goto sendquery_cleanup;
1183 }
1186 }
1187
1192 {
1194
1195 result = PQexec(pset.db, "SAVEPOINT pg_psql_temporary_savepoint");
1197 {
1200 goto sendquery_cleanup;
1201 }
1204 }
1205
1206 if (pset.gdesc_flag)
1207 {
1208 /* Describe query's result columns, without executing it */
1209 OK = DescribeQuery(query, &elapsed_msec);
1210 }
1211 else
1212 {
1213 /* Default fetch-and-print mode */
1214 OK = (ExecQueryAndProcessResults(query, &elapsed_msec, &svpt_gone, false, 0, NULL, NULL) > 0);
1215 }
1216
1217 if (!OK && pset.echo == PSQL_ECHO_ERRORS)
1218 pg_log_info("STATEMENT: %s", query);
1219
1220 /* If we made a temporary savepoint, possibly release/rollback */
1222 {
1223 const char *svptcmd = NULL;
1224
1226
1227 switch (transaction_status)
1228 {
1229 case PQTRANS_INERROR:
1230 /* We always rollback on an error */
1231 svptcmd = "ROLLBACK TO pg_psql_temporary_savepoint";
1232 break;
1233
1234 case PQTRANS_IDLE:
1235 /* If they are no longer in a transaction, then do nothing */
1236 break;
1237
1238 case PQTRANS_INTRANS:
1239
1240 /*
1241 * Release our savepoint, but do nothing if they are messing
1242 * with savepoints themselves
1243 */
1244 if (!svpt_gone)
1245 svptcmd = "RELEASE pg_psql_temporary_savepoint";
1246 break;
1247
1248 case PQTRANS_ACTIVE:
1249 case PQTRANS_UNKNOWN:
1250 default:
1251 OK = false;
1252 /* PQTRANS_UNKNOWN is expected given a broken connection. */
1254 pg_log_error("unexpected transaction status (%d)",
1256 break;
1257 }
1258
1259 if (svptcmd)
1260 {
1262
1265 {
1268 OK = false;
1269
1270 goto sendquery_cleanup;
1271 }
1273 }
1274 }
1275
1276 /* Possible microtiming output */
1277 if (timing)
1279
1280 /* check for events that may occur during query execution */
1281
1283 PQclientEncoding(pset.db) >= 0)
1284 {
1285 /* track effects of SET CLIENT_ENCODING */
1288 SetVariable(pset.vars, "ENCODING",
1290 }
1291
1293
1294 /* perform cleanup that should occur after any attempted query */
1295
1297
1298 /* global cancellation reset */
1300
1301 /* reset \g's output-to-filename trigger */
1302 if (pset.gfname)
1303 {
1304 free(pset.gfname);
1305 pset.gfname = NULL;
1306 }
1307
1308 /* restore print settings if \g changed them */
1309 if (pset.gsavepopt)
1310 {
1313 }
1314
1315 /* clean up after extended protocol queries */
1317
1318 /* reset \gset trigger */
1319 if (pset.gset_prefix)
1320 {
1323 }
1324
1325 /* reset \gdesc trigger */
1326 pset.gdesc_flag = false;
1327
1328 /* reset \gexec trigger */
1329 pset.gexec_flag = false;
1330
1331 /* reset \crosstabview trigger */
1332 pset.crosstab_flag = false;
1333 for (i = 0; i < lengthof(pset.ctv_args); i++)
1334 {
1336 pset.ctv_args[i] = NULL;
1337 }
1338
1339 return OK;
1340}
static bool DescribeQuery(const char *query, double *elapsed_msec)
Definition common.c:1352
static void PrintNotifications(void)
Definition common.c:742
void clean_extended_state(void)
Definition common.c:2703
static bool command_no_begin(const char *query)
Definition common.c:2307
#define lengthof(array)
Definition c.h:873
void restorePsetInfo(printQueryOpt *popt, printQueryOpt *save)
Definition command.c:5705
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
int PQclientEncoding(const PGconn *conn)
void pg_free(void *ptr)
PGTransactionStatusType
Definition libpq-fe.h:152
@ PQTRANS_INTRANS
Definition libpq-fe.h:155
@ PQTRANS_IDLE
Definition libpq-fe.h:153
@ PQTRANS_ACTIVE
Definition libpq-fe.h:154
@ PQTRANS_UNKNOWN
Definition libpq-fe.h:157
@ PQTRANS_INERROR
Definition libpq-fe.h:156
#define pg_encoding_to_char
Definition pg_wchar.h:483
@ 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(), fb(), 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(), result, SetCancelConn(), SetVariable(), _psqlSettings::singlestep, _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 2562 of file common.c.

2563{
2564 const char *val;
2565
2566 if (!pset.db)
2567 return NULL;
2568
2569 val = PQparameterStatus(pset.db, "session_authorization");
2570 if (val)
2571 return val;
2572 else
2573 return PQuser(pset.db);
2574}
char * PQuser(const PGconn *conn)

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

Referenced by get_prompt().

◆ SetPipelineVariables()

static void SetPipelineVariables ( void  )
static

Definition at line 536 of file common.c.

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

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
160 }
161
164
165 /* Adjust SIGPIPE handling appropriately: ignore signal if 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:3061
bool queryFoutPipe
Definition settings.h:106

References fb(), fout, openQueryOutputFile(), pset, _psqlSettings::queryFout, _psqlSettings::queryFoutPipe, restore_sigpipe_trap(), set_sigpipe_trap_state(), and SetShellResultVariables().

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

◆ SetResultVariables()

static void SetResultVariables ( PGresult result,
bool  success 
)
static

Definition at line 478 of file common.c.

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

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

Referenced by DescribeQuery(), and ExecQueryAndProcessResults().

◆ SetShellResultVariables()

void SetShellResultVariables ( int  wait_result)

Definition at line 518 of file common.c.

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

References buf, fb(), 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 {
96 {
97 if (*is_pipe)
99 }
100 else
101 return false;
102 }
103 return true;
104}
void disable_sigpipe_trap(void)
Definition print.c:3025

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

Referenced by ExecQueryAndProcessResults().

◆ skip_white_space()

static const char * skip_white_space ( const char query)
static

Definition at line 2243 of file common.c.

2244{
2245 int cnestlevel = 0; /* slash-star comment nest level */
2246
2247 while (*query)
2248 {
2249 int mblen = PQmblenBounded(query, pset.encoding);
2250
2251 /*
2252 * Note: we assume the encoding is a superset of ASCII, so that for
2253 * example "query[0] == '/'" is meaningful. However, we do NOT assume
2254 * that the second and subsequent bytes of a multibyte character
2255 * couldn't look like ASCII characters; so it is critical to advance
2256 * by mblen, not 1, whenever we haven't exactly identified the
2257 * character we are skipping over.
2258 */
2259 if (isspace((unsigned char) *query))
2260 query += mblen;
2261 else if (query[0] == '/' && query[1] == '*')
2262 {
2263 cnestlevel++;
2264 query += 2;
2265 }
2266 else if (cnestlevel > 0 && query[0] == '*' && query[1] == '/')
2267 {
2268 cnestlevel--;
2269 query += 2;
2270 }
2271 else if (cnestlevel == 0 && query[0] == '-' && query[1] == '-')
2272 {
2273 query += 2;
2274
2275 /*
2276 * We have to skip to end of line since any slash-star inside the
2277 * -- comment does NOT start a slash-star comment.
2278 */
2279 while (*query)
2280 {
2281 if (*query == '\n')
2282 {
2283 query++;
2284 break;
2285 }
2286 query += PQmblenBounded(query, pset.encoding);
2287 }
2288 }
2289 else if (cnestlevel > 0)
2290 query += mblen;
2291 else
2292 break; /* found first token */
2293 }
2294
2295 return query;
2296}

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

Referenced by command_no_begin().

◆ standard_strings()

bool standard_strings ( void  )

Definition at line 2542 of file common.c.

2543{
2544 const char *val;
2545
2546 if (!pset.db)
2547 return false;
2548
2549 val = PQparameterStatus(pset.db, "standard_conforming_strings");
2550
2551 if (val && strcmp(val, "on") == 0)
2552 return true;
2553
2554 return false;
2555}

References _psqlSettings::db, fb(), 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 799 of file common.c.

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

References fb(), free, _psqlSettings::gset_prefix, i, pg_log_error, pg_log_warning, PQfname, PQgetisnull, PQgetvalue, PQnfields, PQntuples, pset, psprintf(), result, 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 2677 of file common.c.

2678{
2679 /* The connection URI must start with either of the following designators: */
2680 static const char uri_designator[] = "postgresql://";
2681 static const char short_uri_designator[] = "postgres://";
2682
2684 sizeof(uri_designator) - 1) == 0)
2685 return sizeof(uri_designator) - 1;
2686
2688 sizeof(short_uri_designator) - 1) == 0)
2689 return sizeof(short_uri_designator) - 1;
2690
2691 return 0;
2692}
static const char short_uri_designator[]
Definition fe-connect.c:456
static const char uri_designator[]
Definition fe-connect.c:455

References connstr, fb(), short_uri_designator, and uri_designator.

Referenced by recognized_connection_string().

Variable Documentation

◆ sigint_interrupt_enabled

◆ sigint_interrupt_jmp