Postgres - 将元素作为数组重复 N 次

eye*_*mew 3 postgresql postgres-9.6

例如,如果元素是'hi',而N3,我需要一个可以在SELECT返回以下数组的查询中使用的 PostgreSQL 片段:

['hi', 'hi', 'hi']
Run Code Online (Sandbox Code Playgroud)

Tav*_*ole 7

Postgresarray_fill为此目的提供了,例如:

SELECT array_fill('hi'::text, '{3}');
SELECT array_fill('hi'::text, array[3]);
Run Code Online (Sandbox Code Playgroud)

这两个示例是等效的,但如果您希望用变量替换维度,则第二种形式更方便3

另请参阅: https: //www.postgresql.org/docs/current/functions-array.html