This module provides utility functions for maintaining partitioned kdb+ database tables. It is not designed for use with splayed tables that are not partitioned, single-file tables, or in-memory tables.
Before using these functions on a production database, it is strongly recommended to test them on a sample database. You can create a sample database using the buildPersistedDB function from the KX Datagen module.
If you make changes to an existing database, you must reload it using (\l .) to apply the modifications.
Ensure the module is installed and available on QPATH.
dbmaint:use`kx.dbmaintAdds a column to a database table.
dbmaint.addCol[db;tname;cname;default]or for new symbol columns
dbmaint.addCol[db;tname;cname;default;domain]| Parameter | Type | Description |
|---|---|---|
db |
string, symbol or fileSymbol |
Path to the database root. |
tname |
symbol |
Table name. |
cname |
symbol |
Column name. |
default |
any |
Default value for the column. |
domain |
symbol |
Optional: Sym file (domain) name (only used if the column is of symbol type). |
Add a new column (newCol) to the trade table with a default value of 10:
dbmaint.addCol[`db;`trade;`newCol;10]Add a new column (newSymCol) to the trade table, within all partitions under db, with a default value of `abc, enumerated against mySym:
dbmaint.addCol[`db;`trade;`newSymCol;`abc;`mySym]Adds missing columns across all partitions of a table.
dbmaint.addMissingCols[db;tname;goodTdir]| Parameter | Type | Description |
|---|---|---|
db |
string, symbol or fileSymbol |
Path to the database root. |
tname |
symbol |
Table name. |
goodTdir |
string, symbol or fileSymbol |
Path to a table directory that contains all required columns. |
Add any missing columns to the trade table that exist in the 2025.12.17 partition but are missing from other partitions:
dbmaint.addMissingCols[`db;`trade;"db/2025.12.17/trade"]Adds a new table to all partitions of a database.
dbmaint.addTab[db;domain;tname;schema]| Parameter | Type | Description |
|---|---|---|
db |
string, symbol or fileSymbol |
Path to the database root. |
domain |
symbol |
Sym file (domain) name. |
tname |
symbol |
Table name. |
schema |
table |
Schema of the new table. |
Add the quote table to the database:
dbmaint.addTab[`db;`quote;([] sym:`$(); ap:"f"$(); bp:"f"$())]Applies a function to a column across all partitions of a table.
dbmaint.fnCol[db;tname;cname;fn]| Parameter | Type | Description |
|---|---|---|
db |
string, symbol or fileSymbol |
Path to the database root. |
tname |
symbol |
Table name. |
cname |
symbol |
Column name. |
fn |
function |
Unary function to apply to the column. |
dates |
date|date[] |
Optional: date(s) to restrict to. |
Multiply the price column in the trade table by 2:
dbmaint.fnCol[`db;`trade;`price;2*]Negate the trade prices for yesterday
dbmaint.fnCol[`db;`trade;`price;neg;.z.D-1]Make the characters in the alpha column of the trade table uppercase:
dbmaint.fnCol[`db;`trade;`alpha;upper]Casts a column to a specified type.
dbmaint.castCol[db;tname;cname;typ]| Parameter | Type | Description |
|---|---|---|
db |
fileSymbol |
Path to the database root. |
tname |
symbol |
Table name. |
cname |
symbol |
Column name. |
type |
short|char|symbol |
Type to cast the column to. |
dates |
date|date[] |
Optional: date(s) to restrict to. |
Cast the size column in the trade table to a float:
dbmaint.castCol[`db;`trade;`size;"f"]Sets an attribute on a column.
dbmaint.setAttr[db;tname;cname;attrb]| Parameter | Type | Description |
|---|---|---|
db |
string, symbol or fileSymbol |
Path to the database root. |
tname |
symbol |
Table name. |
cname |
symbol |
Column name. |
attrb |
symbol |
Attribute (s, u, p, g). |
dates |
date|date[] |
Optional: date(s) to restrict to. |
Apply the parted attribute to the sym column in the trade table:
dbmaint.setAttr[`db;`trade;`sym;`p]Removes an attribute from a column.
dbmaint.rmAttr[db;tname;cname]| Parameter | Type | Description |
|---|---|---|
db |
string, symbol or fileSymbol |
Path to the database root. |
tname |
symbol |
Table name. |
cname |
symbol |
Column name. |
dates |
date|date[] |
Optional: date(s) to restrict to. |
Remove the attribute from the sym column in the trade table:
dbmaint.rmAttr[`db;`trade;`sym]Copies a column across all partitions of a table.
dbmaint.copyCol[db;tname;srcCol;dstCol]| Parameter | Type | Description |
|---|---|---|
db |
string, symbol or fileSymbol |
Path to the database root. |
tname |
symbol |
Table name. |
srcCol |
symbol |
Name of the column to copy. |
dstCol |
symbol |
Name of the new column to create. |
Copy the size column to a new column called sizeCopy in the trade table:
dbmaint.copyCol[`db;`trade;`size;`sizeCopy]Deletes a column from a database table.
dbmaint.delCol[db;tname;cname]| Parameter | Type | Description |
|---|---|---|
db |
string, symbol or fileSymbol |
Path to the database root. |
tname |
symbol |
Table name. |
cname |
symbol |
Column name. |
Delete the column oldCol from the trade table:
dbmaint.delCol[`db;`trade;`oldCol]Deletes a table from a database.
dbmaint.delTab[db;tname]| Parameter | Type | Description |
|---|---|---|
db |
string, symbol or fileSymbol |
Path to the database root. |
tname |
symbol |
Table name. |
Delete the trade table:
dbmaint.delTab[`db;`trade]Checks if a given column exists in the table.
dbmaint.hasCol[db;tname;cname]| Parameter | Type | Description |
|---|---|---|
db |
string, symbol or fileSymbol |
Path to the database root. |
tname |
symbol |
Table name. |
cname |
symbol |
Column name. |
(bool) 1b if the column exists in the table, 0b otherwise.
Check if the trade table has the size column:
dbmaint.hasCol[`db;`trade;`size]Lists all column names of the specified table.
dbmaint.listCols[db;tname]| Parameter | Type | Description |
|---|---|---|
db |
string, symbol or fileSymbol |
Path to the database root. |
tname |
symbol |
Table name. |
(symbols) Column names.
List the columns of the trade table:
q)dbmaint.listCols[`db;`trade]
`time`sym`size`price`company`movesRenames a column across all partitions of a table.
dbmaint.renameCol[db;tname;old;new]| Parameter | Type | Description |
|---|---|---|
db |
string, symbol or fileSymbol |
Path to the database root. |
tname |
symbol |
Table name. |
old |
symbol |
Current column name. |
new |
symbol |
New column name. |
Rename the size column to sizeRenamed in the trade table:
dbmaint.renameCol[`db;`trade;`size;`sizeRenamed]Renames a table in all partitions.
dbmaint.renameTab[db;old;new]| Parameter | Type | Description |
|---|---|---|
db |
string, symbol or fileSymbol |
Path to the database root. |
old |
symbol |
Current table name. |
new |
symbol |
New table name. |
Rename the trade table to quote:
dbmaint.renameTab[`db;`trade;`quote]Reorders the columns across all partitions of a table.
dbmaint.reorderCols[db;tname;order]| Parameter | Type | Description |
|---|---|---|
db |
string, symbol or fileSymbol |
Path to the database root. |
tname |
symbol |
Table name. |
order |
symbols |
New column order (some or all columns). |
Reorder the columns of the trade table so that sym, time, and price appear first:
dbmaint.reorderCols[`db;`trade;`sym`time`price]