Skip to content

Coding Standards

Mohammad Rasoul Kazemi edited this page Nov 26, 2026 · 2 revisions

Coding Standards

The authority is coding-standards.md; this page summarises it. Each rule names the tool or review step that enforces it, and a rule without a tool is checked in review. The document still marks a few commands "to be confirmed"; where it names a CI job, no CI exists (see Development and Testing), so the check is run by hand. Tool versions: Go 1.27.3 (the go directive of both go.mod files) and Node.js 24.22.1 (pinned for the release build in versions.env; the engines field of the console asks for 22 or newer).

Cross-cutting rules

  • English only. Code, identifiers, comments, commit messages, documents, file names, test data and log messages. Plain prose, no marketing language, no emojis.
  • Comments say why. A comment that repeats the code is deleted; one that records a constraint, an upstream behaviour, a failure mode or a decision is required.
  • No secrets in the repository, including tests and examples. Test credentials are generated at test time.
  • Licence header. Every new KingresDB source file starts with two header lines in the first comment, SPDX-License-Identifier: AGPL-3.0-only and Copyright (c) 2026, RooyeKhat Media Co, in the comment syntax of the file type (/* */ in C, -- in SQL, // in Go and TypeScript, <!-- --> in Markdown, # in scripts and YAML). Files copied from upstream keep their upstream header and are not re-headed. KingresDB's own code is AGPL-3.0-only (ADR 0024); PostgreSQL's code keeps the PostgreSQL License.
  • Third-party code is not copied in without a decision of its own; a dependency needs a licence that can be combined with AGPL-3.0; Citus code, tests and comments are never copied or closely paraphrased (Citus is AGPL-3, and the project uses its documentation only as a source of concepts).
  • Needs-verification discipline. A design item marked [Needs verification] is checked against the PostgreSQL source before code depends on it, and the result goes in the work log.
  • Commits. One logical change per commit. Core patches follow extra rules (Branching and Synchronisation).

C (extensions and core patches)

KingresDB C code must look like PostgreSQL C code.

  • Language level: C99 only; declarations at the top of the block; /* */ comments only; PostgreSQL types and macros (bool, Oid, Datum, Assert); no -Wno-* flag to hide warnings.
  • Formatting: pgindent with the tree's src/tools/pgindent/typedefs.list; new typedefs are added to that file in a separate commit (the only upstream-file change that needs no patch ID). Tabs are 4 columns. Line endings are LF (core.autocrlf=false, core.eol=lf, .gitattributes, .editorconfig).
  • Naming: non-static symbols use kingres_ (functions, variables), Kingres (types) or KINGRES_ (macros); inside the extensions kingres_shard_ and kingres_ledger_. SQL-callable functions are kingres_<name> with PG_FUNCTION_INFO_V1. Every GUC carries the extension prefix and the prefix is reserved with MarkGUCPrefixReserved() in _PG_init.
  • Errors: ereport(ERROR, (errcode(...), errmsg(...))) with a specific SQLSTATE; ERRCODE_INTERNAL_ERROR is not used for conditions a user can cause; elog only for "cannot happen". Messages follow the PostgreSQL style guide (short, lower case, no trailing period; errdetail and errhint are sentences). Each error path has a test that asserts its SQLSTATE and message.
  • Memory: palloc/pfree/repalloc, never malloc; long-lived data in a named context under TopMemoryContext or CacheMemoryContext; shared memory requested in shmem_request_hook and initialised in shmem_startup_hook.
  • PG_TRY: prefer resource owners and PG_FINALLY; variables read after PG_CATCH are volatile; never return, goto or break out of a PG_TRY; no ereport(ERROR) under a spinlock.
  • Hooks: save the previous hook in _PG_init and call it (or the standard function) when the query is not yours. A regression test loads another extension that uses the same hook.
  • Files: postgres.h first in every .c; one PG_MODULE_MAGIC; per extension; _PG_init is the only place that installs hooks, defines GUCs and requests shared memory; both a Makefile (PGXS) and a meson.build, listing the same sources, data files (every upgrade script) and tests.
  • Comments: every file starts with a banner (purpose, licence, IDENTIFICATION); every non-trivial function has a comment block before it saying what it does and why. The banner of the extension sources carries Portions Copyright (c) 2026, RooyeKhat Media Co.

One source for both majors

ADR 0023 and section 1.13 of the standards:

  • Each extension has one compatibility header, src/pg_compat.h, holding every difference between PostgreSQL 18 and 19 (a renamed function, a changed signature, a moved header, a changed struct member). It exists for kingres_shard, kingres_ledger, kingres_sketch and kingres_columnar.
  • Nothing outside pg_compat.h tests PG_VERSION_NUM or PG_MAJORVERSION_NUM. Code calls a macro or static inline function that the header defines for each major, named after what the code means. Check: grep -rn PG_VERSION_NUM contrib/kingres_* lists only pg_compat.h files (and one compatibility header of a columnar test helper, t/kcevents/kcevents_compat.h).
  • A feature of the newer major alone lives in its own file; on the older major its SQL function raises a clear "needs PostgreSQL N" error. SQL scripts are the same on every major. Expected output that legitimately differs uses expected/<test>_1.out.
  • No compiler warning on any major; kingres/dev/ext-majors.sh builds with -Werror on each.

SQL install and upgrade scripts

  • The control file's default_version names the current version; the install script creates the full current state; each version has an upgrade script kingres_<name>--<old>--<new>.sql. Scripts start with the \echo Use "CREATE EXTENSION ..." \quit guard.
  • A released script is never edited. Ledger hash formats are versioned with hash_version, and an upgrade must never change how existing hashes are computed.

SQL and PL/pgSQL

  • All objects are in the schema kingres; names are lower case snake_case; constraints and indexes are named explicitly.
  • Every function sets search_path explicitly (SET search_path = pg_catalog, kingres); SECURITY DEFINER only when needed, with a dedicated owner, REVOKE ALL ... FROM PUBLIC, explicit GRANT EXECUTE and validated arguments.
  • Dynamic SQL uses format() with %I and %L; no SELECT * in interfaces; COMMENT ON for every public object.

Go (control plane, verifier)

  • cmd/<binary>/main.go per executable and internal/ for everything else; one go.mod per module (control plane, verifier).
  • gofmt without a diff, go vet ./..., golangci-lint run, tests with -race.
  • context.Context is the first parameter of anything that does I/O or blocks; errors are returned and wrapped with %w; logging is log/slog only and never logs secrets; no global mutable state and no work in init(); interfaces are defined where they are used.
  • Tests are table-driven, next to the code, with no sleeping to wait for conditions.
  • Standard library first; a new module needs a licence check, a maintenance check and a note naming module, version, licence and reason (kingres/control-plane/DEPENDENCIES.md and kingres/web/DEPENDENCIES.md hold the lists).
  • The REST server is written against openapi.yaml; a handler that is not in the spec is a bug.

TypeScript and Vue (web console)

  • Vue 3 with <script setup lang="ts"> (ADR 0017); TypeScript strict, no any, no @ts-ignore.
  • ESLint (flat config) and Prettier; the API client is generated from openapi.yaml, never written by hand, and the checks fail if the generated files are stale.
  • Accessibility rules: semantic HTML, keyboard operation with a visible focus, labelled controls, status never by colour alone (badges carry text), dialogs that trap and restore focus. Tests find controls by role and label.
  • User-visible text is English.

Mutants and the tests that guard fixes

A fix is trusted only if its test fails without it. Authors and reviewers apply one-line mutants (the fix removed or a condition changed) and require a test to fail; a test that passes with the fix removed is a finding. See Testing Strategy and Review Process.

Documentation rules

From documentation.md: say each thing once and link; keep sourced facts, recommendations and needs-verification items apart; do not state commands or numbers that were not checked; describe the current state in present tense (history belongs in the work log and ADR statuses). An ADR holds one decision with context, alternatives, consequences and status, is not edited once accepted except for its status, and is replaced by a new ADR that says "Supersedes ADR NNNN". A work-log entry has the date and title, what was done, how it was verified (including "not verified"), needs-verification items checked, decisions and deviations, what is still open, and core patches touched.

Sources

Clone this wiki locally