Skip to content

Latest commit

 

History

7 Commits

Folders and files

Repository files navigation

Easy random sampling in PostgreSQL

  • 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 postgres engine version 18.6 , plus 19.0-R1 in the Database Preview Environment

SQL formatting

SQL formatting conventions

Current or potential comma-separated lists

  • 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

Boolean expressions

  • 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

Operators and keywords

  • Align or space out pairs based on the longer possibility

      =  >    asc   true
     !=  >=  desc  false

Table and column references

  • Always define a range variable (a table alias, in familiar language) when using a values list or a call to a function (like generate_series in 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 like exists() or in a common table expression where the column list has been specified immediately above, for example, in a union

Licenses

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 @)

Releases

Contributors

Languages