Execute Code
This page explains how to execute q, Python, and SQL code using the kdb Visual Studio Code Extension.
The extension allows you to run code directly from your workspace against:
- Local or remote kdb/q processes (My q connections)
- A KDB-X runtime
- kdb Insights Enterprise deployments
Execution is available for .q, .py, and .sql files, as well as through the REPL and other tools such as workbooks.
Execute code against kdb processes (q and Python)
When working with .q or .py files, the extension provides additional execution actions in the editor right-click menu or command palette:
-
Execute entire file. This action takes the current file and executes it against the active connection. Results are displayed in the Output window. Returned data is displayed in the KDB Results window.
-
Execute current selection. This action takes the current selection (or current line if nothing is selected) and executes it against the active connection. Results are displayed in the Output window and/or the KDB Results window.
-
Execute current block. This action selects the q expression under the cursor and executes it against the active connection. Results are displayed in the Output window and/or the KDB Results window.
Execute Python code against KDB-X connections
The kdb VS Code Extension also supports running Python code against q backends, with the following considerations:
-
Execution context. A Python variable is defined in the remote process
_kx_execution_context, which means you need to explicitly accept it to avoid implicit changes to the remote process, but you don’t need to interact with it directly.Python variables you create stay inside the Python context only. They do not change anything in the q process unless you intentionally send data back to q. In other words, running Python code will not accidentally modify q tables, variables, or state. Any updates to q must be done explicitly (for example, by assigning a variable into q or pushing data back).
-
PyKX requirement. To write and execute Python code against KDB-X connections, make sure that
pykxis loaded into the q process. If.pykxis undefined, it returns the following error:.pykx is not defined: please load pykx.PyKX is a Python-first interface to KDB-X and q. It helps you query and analyze huge amounts of in-memory and on-disk time-series data, significantly faster than other libraries. For more information on getting started with PyKX, refer to the PyKX quickstart.
Execute code against kdb Insights Enterprise
kdb Insights Enterprise provides advanced connectivity options and enterprise-level API endpoints. These features allow you to query data and interact with the platform in ways that go beyond standard kdb processes.
To get started, ensure that:
- An instance of kdb Insights Enterprise is running.
- You have created a connection using the kdb VS Code extension.
Execute arbitrary code
You can execute custom code against kdb Insights Enterprise. This code runs in a user-specific scratchpad process within the deployment:
- The scratchpad, which is essentially a q process, is automatically created when you send your first code execution request while connected to a kdb Insights Enterprise instance.
- It remains active until it times out or you log out.
This approach provides a flexible environment for testing and running ad hoc queries without affecting production processes.
Concurrent code execution and querying
kdb Insights Enterprise deployments contain one or more Data Access Processes (DAPs) distributed across tiers such as:
- RDB (real-time database)
- IDB (in-memory database)
- HDB (historical database)
Each tier (RDB, IDB, HDB) contains multiple Data Access Processes (DAPs) that handle queries in parallel. For example, if the RDB tier has three replicas (rdb-0, rdb-1, rdb-2), and no specific replica is targeted, queries are routed to any available process. This allows multiple users to execute queries simultaneously. When a process becomes free, it immediately handles the next incoming query, ensuring efficient resource utilization and minimal delays.
Target specific replicas
Using the kdb VS Code Extension, you can execute code on a specific DAP. This enables you to target specific replicas within the RDB, IDB, or HDB tiers for query execution.
To select a replica:
- Choose the desired tier (RDB, IDB, HDB).
- Select the specific replica from the list of available options, such as:
demo-ui-fx rdb-0demo-ui-fx idb-1
Once selected, queries execute on that specific replica. This is useful not only for performance tuning and load distribution, but also when developing or testing UDAs. By sending queries to the same DAP each time, you ensure that your UDA code is executed in a consistent runtime environment, which helps avoid differences in state or configuration across replicas.
View replica details
When connected to kdb Insights version 1.14.2 or higher, you can view detailed information about the available replicas for each tier (RDB, IDB, HDB). This allows you to choose a specific replica for your query.
The list includes the individual replicas available for execution, and may look similar to the example below:
- scratchpad
- demo-ui-fx idb
- demo-ui-fx rdb
- demo-ui-fx hdb
- demo-ui-fx idb-0
- demo-ui-fx idb-1
- demo-ui-fx idb-2
- …
- demo-ui-fx rdb-0
- demo-ui-fx rdb-1
- demo-ui-fx rdb-2
- …
- demo-ui-fx hdb-0
- demo-ui-fx hdb-1
- demo-ui-fx hdb-2
- …
If you are using an older kdb Insights version (1.14.1 or lower), replica information is not available. However, you can still run queries on a specific tier of a database (RDB, IDB, HDB), but don’t have the option to target a specific replica. This ensures that the feature works even if you are using older versions of Insights.
Next steps
- Learn about data sources
- See the reference card for full details on execution