- Source code to accompany my November 24, 2026 article, "A random sampling index in PostgreSQL - Avoid a DevOps rat race" ( LinkedIn | blog ), about defining a systematic stratified sampling index in PostgreSQL, to get accurate and precise results for a large data set without bigger hardware or more hardware
- Supports PostgreSQL 18 and 19
- Tested in Amazon RDS, with
postgresengine version18.6, plus19.0-R1in the Database Preview Environment
SQL formatting conventions
- Context examples:
select:from,group by,order by - 1 item per line; indent 1 extra space for comma
- Conspicuous leading comma prevents syntax errors when items are added
- Put the comma on a line by itself if entries are likely to be added or removed at the beginning, commented out individually, or copied and pasted
- Context examples:
select:where,having - No extra indentation unless and until logical operators (
not,or,and) are added - Then, use line breaks, indentation, and parentheses so that the expression can be read naturally and parallel arms of the expression can easily be recognized
-
Align or space out pairs based on the longer possibility
= > asc true != >= desc false
- Always define a range variable (a table alias, in familiar language) when
using a
valueslist or a call to a function (likegenerate_seriesin PostgreSQL) in place of a table name - Always define a column alias if the column would otherwise have no known name, or an ambiguous name
- For clarity, follow these rules even if the column is not currently
referenced elsewhere, or if it is referenced based solely on its order (as in
the case of
insert) - Generally, avoid
*for a column list - It is OK to use
select *with quantifiers likeexists()or in a common table expression where the column list has been specified immediately above, for example, in aunion
| Scope | Link | Included Copy |
|---|---|---|
| Source code, and source code in documentation | GNU General Public License (GPL) 3.0 | LICENSE-CODE.md |
| Documentation, including this ReadMe file | GNU Free Documentation License (FDL) 1.3 | LICENSE-DOC.md |
Copyright Paul Marcelin
Contact: marcelin at cmu.edu (replace "at" with @)