select open change scope Open full search

PG.CENTER connects PostgreSQL documentation, reference, and ecosystem knowledge. Maintained by Pigsty.

FUNCTIONS / ARRAY FUNCTIONS AND OPERATORS

Returns an array of n items randomly selected from array.

Signatures & examples

array_sample ( array anyarray, n integer ) → anyarray
Returns an array of n items randomly selected from array. n may not exceed the length of array's first dimension. If array is multi-dimensional, an item is a slice having a given first subscript.
array_sample(ARRAY[1,2,3,4,5,6], 3) → {2,6,1}
array_sample(ARRAY[[1,2],[3,4],[5,6]], 2) → {{5,6},{1,2}}
Version history 1
  1. PG 15 → 16added

Definition snapshot: 2026-09-14T13:14:25.684213+00:00@99e6866c · English manual source