@@ -106,7 +106,7 @@ static backslashResult exec_command_lo(PsqlScanState scan_state, bool active_bra
106106 const char * cmd );
107107static backslashResult exec_command_out (PsqlScanState scan_state , bool active_branch );
108108static backslashResult exec_command_print (PsqlScanState scan_state , bool active_branch ,
109- PQExpBuffer query_buf );
109+ PQExpBuffer query_buf , PQExpBuffer previous_buf );
110110static backslashResult exec_command_password (PsqlScanState scan_state , bool active_branch );
111111static backslashResult exec_command_prompt (PsqlScanState scan_state , bool active_branch ,
112112 const char * cmd );
@@ -362,7 +362,8 @@ exec_command(const char *cmd,
362362 else if (strcmp (cmd , "o" ) == 0 || strcmp (cmd , "out" ) == 0 )
363363 status = exec_command_out (scan_state , active_branch );
364364 else if (strcmp (cmd , "p" ) == 0 || strcmp (cmd , "print" ) == 0 )
365- status = exec_command_print (scan_state , active_branch , query_buf );
365+ status = exec_command_print (scan_state , active_branch ,
366+ query_buf , previous_buf );
366367 else if (strcmp (cmd , "password" ) == 0 )
367368 status = exec_command_password (scan_state , active_branch );
368369 else if (strcmp (cmd , "prompt" ) == 0 )
@@ -955,7 +956,7 @@ exec_command_edit(PsqlScanState scan_state, bool active_branch,
955956 if (fname )
956957 canonicalize_path (fname );
957958
958- /* Applies to previous query if current buffer is empty */
959+ /* If query_buf is empty, recall previous query for editing */
959960 copy_previous_query (query_buf , previous_buf );
960961
961962 if (do_edit (fname , query_buf , lineno , NULL ))
@@ -1827,12 +1828,19 @@ exec_command_out(PsqlScanState scan_state, bool active_branch)
18271828 */
18281829static backslashResult
18291830exec_command_print (PsqlScanState scan_state , bool active_branch ,
1830- PQExpBuffer query_buf )
1831+ PQExpBuffer query_buf , PQExpBuffer previous_buf )
18311832{
18321833 if (active_branch )
18331834 {
1835+ /*
1836+ * We want to print the same thing \g would execute, but not to change
1837+ * the query buffer state; so we can't use copy_previous_query().
1838+ * Also, beware of possibility that buffer pointers are NULL.
1839+ */
18341840 if (query_buf && query_buf -> len > 0 )
18351841 puts (query_buf -> data );
1842+ else if (previous_buf && previous_buf -> len > 0 )
1843+ puts (previous_buf -> data );
18361844 else if (!pset .quiet )
18371845 puts (_ ("Query buffer is empty." ));
18381846 fflush (stdout );
@@ -2549,9 +2557,14 @@ exec_command_write(PsqlScanState scan_state, bool active_branch,
25492557 {
25502558 int result ;
25512559
2560+ /*
2561+ * We want to print the same thing \g would execute, but not to
2562+ * change the query buffer state; so we can't use
2563+ * copy_previous_query(). Also, beware of possibility that buffer
2564+ * pointers are NULL.
2565+ */
25522566 if (query_buf && query_buf -> len > 0 )
25532567 fprintf (fd , "%s\n" , query_buf -> data );
2554- /* Applies to previous query if current buffer is empty */
25552568 else if (previous_buf && previous_buf -> len > 0 )
25562569 fprintf (fd , "%s\n" , previous_buf -> data );
25572570
@@ -2602,7 +2615,7 @@ exec_command_watch(PsqlScanState scan_state, bool active_branch,
26022615 free (opt );
26032616 }
26042617
2605- /* Applies to previous query if current buffer is empty */
2618+ /* If query_buf is empty, recall and execute previous query */
26062619 copy_previous_query (query_buf , previous_buf );
26072620
26082621 success = do_watch (query_buf , sleep );
0 commit comments