12.2 SQLPlus History command – features and fumbles

Yes, there’s been a lot of hoopla about the HISTORY capability in 12.2 SQlPlus, and I know my friend Gokhan Atil has written about it too. So I’m just gonna share this bit for my team and my feedback on it

SQLplus always lacked the history capability like U/Linux shell history. Now in 12.2 SQLPlus its here. Let me describe a little bit of the feature and its quirks.
Here’s what SQLPlus help has to say about the functionality

SQL> help hist

 HISTORY
 -------

 Stores, lists, executes, edits of the commands
 entered during the current SQL*Plus session.

 HIST[ORY] [N {RUN | EDIT | DEL[ETE]}] | [CLEAR]

 N is the entry number listed in the history list.
 Use this number to recall, edit or delete the command.

 Example:
 HIST 3 RUN - will run the 3rd entry from the list.

 HIST[ORY] without any option will list all entries in the list.

So, let’s walk thru this thing:

[oracle@oracle122 admin]$ sqlplus "/ as sysdba"
SQL*Plus: Release 12.2.0.1.0 Production on Sat Mar 4 14:43:55 2017

Copyright (c) 1982, 2016, Oracle.  All rights reserved.
Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

To enable SQLPlus history, you set "set history on" at the SQLPlus prompt. 
However, it has to be set each time you connect to SQLPlus. I know my feeble little brain 
will forget when I'm in a hurry and login to SQLPlus, thus, 
I added "set history on" into $ORACLE_HOME/sqlplus/admin/glogin.sql

SQL> hist
SP2-1651: History list is empty.


Since I haven't done anything, thus the history is obviously empty.  Now let's do stuff !!

SQL> select name from v$datafile
  2  ;

NAME
--------------------------------------------------------------------------------
+DATA/NISHAN/DATAFILE/system.257.937616243
+DATA/NISHAN/DATAFILE/sysaux.258.937616333
+DATA/NISHAN/DATAFILE/undotbs1.259.937616357
+DATA/NISHAN/4700A987085B3DFAE05387E5E50A8C7B/DATAFILE/system.267.937616485
+DATA/NISHAN/4700A987085B3DFAE05387E5E50A8C7B/DATAFILE/sysaux.266.937616485
+DATA/NISHAN/DATAFILE/users.260.937616359
+DATA/NISHAN/4700A987085B3DFAE05387E5E50A8C7B/DATAFILE/undotbs1.268.937616485
+DATA/NISHAN/49CF3DA922C680E1E0539C14A8C0E4E3/DATAFILE/system.272.937617037
+DATA/NISHAN/49CF3DA922C680E1E0539C14A8C0E4E3/DATAFILE/sysaux.273.937617039
+DATA/NISHAN/49CF3DA922C680E1E0539C14A8C0E4E3/DATAFILE/undotbs1.271.937617037
+DATA/NISHAN/49CF3DA922C680E1E0539C14A8C0E4E3/DATAFILE/users.275.937617207

11 rows selected.

SQL> hist
  1  select name from v$datafile
     ;

However, all these silly little mistakes get recorded into history too.... and thus my sloppiness gets shown in broad daylight :-(
SQL> hist
  1  select name from v$datafile
     ;
  2  1
  3  2
  4  del

Here's the delete command to remove my sloppiness:

SQL> hist 4 del
SQL> hist
  1  select name from v$datafile
     ;
  2  1
  3  2

Let's run some stuff to populate the history with real commands. Note, that all desc, show, set, select commands get recorded into history. 
This may or may not be a good thing....think bloated-ness

SQL> show parameter mem

NAME				     TYPE	 VALUE
------------------------------------ ----------- ------------------------------
hi_shared_memory_address	     integer	 0
inmemory_adg_enabled		     boolean	 TRUE
inmemory_clause_default 	     string
inmemory_expressions_usage	     string	 ENABLE
inmemory_force			     string	 DEFAULT
inmemory_max_populate_servers	     integer	 2
inmemory_query			     string	 ENABLE
inmemory_size			     big integer 208M
inmemory_trickle_repopulate_servers_ integer	 1
percent
inmemory_virtual_columns	     string	 MANUAL
memory_max_target		     big integer 0
memory_target			     big integer 0
optimizer_inmemory_aware	     boolean	 TRUE
shared_memory_address		     integer	 0

SQL> show parameter inmemory

NAME				     TYPE	 VALUE
------------------------------------ ----------- ------------------------------
inmemory_adg_enabled		     boolean	 TRUE
inmemory_clause_default 	     string
inmemory_expressions_usage	     string	 ENABLE
inmemory_force			     string	 DEFAULT
inmemory_max_populate_servers	     integer	 2
inmemory_query			     string	 ENABLE
inmemory_size			     big integer 208M
inmemory_trickle_repopulate_servers_ integer	 1
percent
inmemory_virtual_columns	     string	 MANUAL
optimizer_inmemory_aware	     boolean	 TRUE

SQL> hist
  1  select name from v$datafile
     ;
  2  1
  3  2
  4  show parameter mem
  5  show parameter sga
  6  show parameter inmemory

SQL> desc v$inmemory_area
 Name					   Null?    Type
 ----------------------------------------- -------- ----------------------------
 POOL						    VARCHAR2(26)
 ALLOC_BYTES					    NUMBER
 USED_BYTES					    NUMBER
 POPULATE_STATUS				    VARCHAR2(26)
 CON_ID 					    NUMBER

Let's say I screw up this query (and I really did)!!

SQL> select * form v$inmemory_area;
select * form v$inmemory_area
         *
ERROR at line 1:
ORA-00923: FROM keyword not found where expected

I could do the old-school way, using the change command as follows:

SQL> c/form/from/
  1* select * from v$inmemory_area
SQL> /

POOL			   ALLOC_BYTES USED_BYTES POPULATE_STATUS
-------------------------- ----------- ---------- --------------------------
    CON_ID
----------
1MB POOL		     166723584		0 DONE
	 1

64KB POOL		      33554432		0 DONE
	 1

1MB POOL		     166723584		0 DONE
	 2


POOL			   ALLOC_BYTES USED_BYTES POPULATE_STATUS
-------------------------- ----------- ---------- --------------------------
    CON_ID
----------
64KB POOL		      33554432		0 DONE
	 2

1MB POOL		     166723584		0 DONE
	 3

64KB POOL		      33554432		0 DONE
	 3


6 rows selected.

SQL> hist
  1  select name from v$datafile
     ;
  2  1
  3  2
  4  show parameter mem
  5  show parameter sga
  6  show parameter inmemory
  ..
  ..
 11  select * form v$inmemory_area;
 12  c/for/from/
 15  /

But I sure wish I could delete multiple lines like this, but alas, I cannot


SQL> hist 11,12,13 del
SP2-1655: History command syntax error.

Anyways, Here's the new way of editing the command in the history :

SQL> hist 
  1  select name from v$datafile
     ;
  2  show parameter mem
  3  show parameter sga
  4  show parameter inmemory
  5  desc v$inmemory_pool
  6  desc v$inmemory_size
  7  esc v$inmemory_area
  8  desc v$inmemory_area
  9  select * form v$inmemory_area;
 10  c/form/from/
 11  /

SQL> hist 9 edit
This pops up the OS editor (vi of course), you edit as you normally would; save/quit, 
and you're back to the SQLPus prompt:

But look what happens after "hist 9 edit", 9 is still the same; its an immutable entry.  
Which suppose it kinda expected, as you shouldn't be able to change history :-) !!

Thus, by editing you're effective storing a new entry in the SQLPlus buffer and 
you'll have to execute this buffer, just like to old school way.  
You can do sqlplus "l" command to list the current buffer

 
This execution adds an new entry for the new command rather than replace the history "9" entry  !!!

SQL> hist 
  1  select name from v$datafile
     ;
  2  show parameter mem
  3  show parameter sga
  4  show parameter inmemory
  5  desc v$inmemory_pool
  6  desc v$inmemory_size
  7  esc v$inmemory_area
  8  desc v$inmemory_area
  9  select * form v$inmemory_area;
 10  c/for/from/
 11  /
 12  select * from  v$inmemory_area;

This feature is good start, but I hope they add more capabilities to this thing as its still rudimentary.