CockroachDB TIL: Volume 10

*This is my series of articles covering short "Today I learned" topics as I work with CockroachDB. Today, we're covering
Previous articles
Topics
- Topic 1: [Generating ULID strings in CockroachDB](#Topic 1: Generating ULID strings in CockroachDB)
- Topic 2: Enable CLI tab completion for column names
Topic 1: Generating ULID strings in CockroachDB
CockroachDB adopted ULID several releases ago as yet another version of UUID type. There is no explicity ULID type in CockroachDB. There are functions available to generate ULID in UUID format. So in case you need to generate lexicographically sortable IDs, you can use the built-in gen_random_ulid() function and there are a several conversion functions to convent from ULID to UUID i.e. ulid_to_uuid and vice versa, i.e. uuid_to_ulid. One point of friction in CockroachDB exists where one needs to generate an actual ULID string and not a UUID formatted ULID. This may or may not be obvious, let's take a look:
Just to be clear, what we need is a string that looks like 01GCCCKGXGF41EDJ8HQENNYA3Y and not like 018318c8-6194-1e75-c9f3-04913630eca8.
SELECT gen_random_ulid();
gen_random_ulid
----------------------------------------
018318c8-6194-1e75-c9f3-04913630eca8
Notice we are generating a ULID, gen_random_ulid() should not be confused with gen_random_uuid(). Accoding to the documentation, the produced output is a valid UUID. So in order for us to return a valid ULID in string form, we have to do the following:
SELECT uuid_to_ulid(gen_random_ulid());
uuid_to_ulid
------------------------------
01GCCCQECQRPVGE4FFENFQJXSA
Topic 2: Enable CLI tab completion for column names
I've been working with a product called FerretDB for a couple of my articles 1 and 2 and I found it frustrating to type the column names as they include a hash in them, i.e. sample_mflix.comments_5886d2d7. Remembering these column names is out of the question. I started digging for a way to enable tab completion in my CLI tool and unfortunatly cockroach CLI does not support it today. Apparently the psql client does support tab completion and I had to give it a try.
Originally it did not activate the tab completion and based on the documenation under the heading Command-Line Editing, I created an .inputrc file in my home directory. I included the following
$if psql
set disable-completion off
$endif
I sourced the file and opened a new terminal, and finally have tab-completion.
Comments