ysql: preserve header upon history file truncation
#9861 aperta il 28 ago 2021
Metriche repository
- Star
- (8229 star)
- Metriche merge PR
- (Merge medio 17g 21h) (92 PR mergiate in 30 g)
Descrizione
Jira Link: DB-2106
ysqlsh history file can be specified by --variable=HISTFILE=/path/to/file. It seems that the default amount of entries (or lines) it keeps is 500. Once it goes past that, upon history file write, it truncates the file. However, it also truncates the header line _HiStOrY_V2_. This makes the file invalid next time it's read. This is possibly related to YB using libedit rather than libreadline (commit d372e8877065dcb97333b460093c0a41ef48128d). I have not tested upstream postgres, but it should be tested with and without --with-libedit-preferred.
Repro:
First, copy 500 dull commands to the clipboard.
for _ in {1..500}; do echo ";"; done | myclipboardcommand
Then, start ysqlsh and paste the contents. End the session with control D.
./bin/ysqlsh --variable=HISTFILE=test_history.sql
I specifically make the commands very short because it makes it more reliable. In my testing, long commands may somehow mush up with adjacent ones.
Check the history file for 500 entries and the header:
wc test_history.sql
501 501 1013 test_history.sql
head -2 test_history.sql
_HiStOrY_V2_
;
Now, add one more entry to the history by running any one command in ysqlsh (with the same HISTFILE variable). Upon closing the session, the history file is truncated. This can be noticed by tail -F test_history.sql while closing the ysqlsh session.
tail: test_history.sql: file truncated
Notice that the header is gone
head -2 test_history.sql
;
;
Try ysqlsh again, and the up arrow or ctrl R will not bring up the history. Interestingly, the current session's history will still be written.
select 1;
tail -2 test_history.sql
;
select\0401;