yugabyte/yugabyte-db

ysql: preserve header upon history file truncation

Open

#9,861 创建于 2021年8月28日

在 GitHub 查看
 (6 评论) (0 反应) (1 负责人)C (1,003 fork)batch import
area/ysqlgood first issuehelp wantedkind/bugpriority/medium

仓库指标

Star
 (8,229 star)
PR 合并指标
 (平均合并 17天 21小时) (30 天内合并 92 个 PR)

描述

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;

贡献者指南