0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【PostgreSQL】年月でパーティショニングするテーブルに単独主キー(UUID)を設定したい

Last updated at Posted at 2025-02-11

はじめに

PostgreSQLではテーブルのパーティショニングがサポートされています。
パーティショニングを行うテーブルに一意制約や主キー制約を設定することは可能ですが、このとき、主キーにはパーティショニングキーを含める必要があります。
今回、パーティショニングしつつ全体で一意なことを保証されたUUIDを用意したかったので主キー・パーティショニングキーにUUIDv7を使う方法を検討しました。
備忘録として残します

解決方法

UUIDv7の値を主キーとし、パーティショニングキーとしても使用することで対処しました。
UUIDv7は先頭48bitがタイムスタンプでソート可能のため、パーティショニングキーとして使用することが可能でした。

create table logs(
  id uuid primary key,
  created_at timestamp with time zone default now(),
  message text not null
) partition by range(id);
create table logs_202501 partition of logs
   for values from('01941f297c00-0000-0000-0000-000000000000') to ('0194becea000-0000-0000-0000-000000000000');
create table logs_202502 partition of logs
   for values from('0194becea000-0000-0000-0000-000000000000') to ('01954f00b000-0000-0000-0000-000000000000');

課題

  • UUIDv7をPostgreSQLで発番する関数はまだPostgreSQLに入っていないので自分で用意する必要がある
  • パーティションテーブルの範囲指定が直感的でない
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?