Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
PostgreSQLの範囲型と排他制約
Search
ISHIDA Akio
December 17, 2013
Programming
26
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
PostgreSQLの範囲型と排他制約
ISHIDA Akio
December 17, 2013
More Decks by ISHIDA Akio
See All by ISHIDA Akio
SQLを実行したときに、PostgreSQLはどのようにデータにアクセスしているのか
iakio
0
300
Prophecyを使った ユニットテスト
iakio
0
15
phpspecで学ぶLondon School TDD
iakio
0
9
XIDを周回させてみよう
iakio
0
23
使いこなそうGUC
iakio
0
9
Other Decks in Programming
See All in Programming
Everything will be SERVERLESS — 信じて運用した10年の経験値 / Everything Will be Serverless — Lessons Learned from 10 Years of Operational Experience
seike460
PRO
1
460
iOS 27でニュースアプリはどう変わる!? 〜日経電子版の新機能対応と、開発事例から〜
lynnswap
1
12k
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
270
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
300
スマートフォンでモールス信号を送受信する 〜スマートフォンのLEDとカメラで作る光通信の設計と実装〜
atsuki_seo
0
180
What We Talk About When We Talk About XP
m_seki
2
680
フロントエンドUIフレームワークのこれまでとこれから
ssssota
5
2.9k
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
4
3.6k
Apple Intelligence を用いた個人情報誤送信防止、及びユーザーリクエスト体験の改善について
yukiny
0
130
マイコン向けの軽量Ruby「PicoRuby」で各種デバイスを制御するネイティブアプリの実現手法
bash0c7
0
440
市販E-Readerを乗っ取れ 〜Embedded Swiftで電子ペーパーガジェットを制御する〜
trickart
0
200
Security issues being discussed on Web Platforms
petamoriken
0
1.2k
Featured
See All Featured
Odyssey Design
rkendrick25
PRO
2
810
A Soul's Torment
seathinner
8
3.6k
The agentic SEO stack - context over prompts
schlessera
0
940
Building the Perfect Custom Keyboard
takai
2
880
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
120
120k
Testing 201, or: Great Expectations
jmmastey
46
8.3k
Build your cross-platform service in a week with App Engine
jlugia
234
19k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
520
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
710
AI: The stuff that nobody shows you
jnunemaker
PRO
10
1.1k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Transcript
PostgreSQLの 範囲型と排他制約 PostgreSQL勉強会@札幌 2013.12.17 @iakio
範囲型とは • 範囲をあらわすデータ型(そのまんま) • 開始と終了を持つ • 含まれているとか、結合・交差とかの演算子が定義さ れている • PostgreSQLでは任意の型を元に新しい範囲型を定義で
きる Developing Time-Oriented Database Applications in SQLではPeriod型、 Temporal Data and the Relational ModelではInterval型と呼ばれているよ
組み込みの範囲型 範囲型 元の型 離散的か int4range integer ◦ int8range bigint ◦
numrange numeric × tsrange timestamp × tstzrange timestamp with timezone × daterange date ◦
範囲型の例(日付の範囲)
こうなります create table members ( birthday date, period daterange, name_en
text ); insert into members(birthday, period, name_en) values ('1988-10-20', '[2001-08-26, 2012-05-18]', 'Risa Niigaki'), ('1988-12-23', '[2003-01-19, 2010-12-15]', 'Eri Kamei'), ('1989-11-11', '[2003-01-19, 2013-05-21]', 'Reina Tanaka'), ('1989-07-13', '[2003-01-19,]', 'Sayumi Michishige'), ('1985-02-26', '[2003-01-19, 2007-06-01]', 'Miki Fujimoto'), ...
範囲型の例(整数型)
こうなります create table level1 ( level int, exp_range int4range, primary
key(level), exclude using gist (exp_range with &&) ); insert into level1 values (1, '[0,11)'), (2, '[11,59)'), (3, '[59,164)');
範囲型の作り方色々 '[0,10)' 0以上10未満 '[0,10]' 0以上10以下 '[0,)' 0以上(上限値なし) '[,)' 上限も下限もなし 'empty'
空の範囲 int4range(0, 10) コンストラクタ関数 [0,10)と同じ int4range(0, 10, '[]') [0,10]と同じ
演算子、関数 http://www.postgresql.jp/document/9.3/html/functions- range.html
離散的、正規化 -- 0以上11未満(11は含まない) -- 離散的な範囲型では正規化される =# select int4range '[0,10]'; int4range
----------- [0,11) (1 行) =# select v, int4range '[,11)' @> v from generate_series(10, 12) as s(v); v | ?column? ----+---------- 10 | t 11 | f 12 | f (3 行)
使用例 -- 二人の在籍期間の重複日数 =# with q(n1, n2, p) as (
select m1.name_en, m2.name_en, m1.period * m2.period from members m1 join members m2 on(m1.name_en < m2.name_en) ) select n1, n2, upper(p) - lower(p) from q where not isempty(p) order by 3; n1 | n2 | ?column? ------------------+-------------------+---------- ... Reina Tanaka | Sayumi Michishige | 3776 Ai Takahashi | Risa Niigaki | 3688 Reina Tanaka | Risa Niigaki | 3408 Risa Niigaki | Sayumi Michishige | 3408
EXCLUDE制約 • UNIQUE制約は、同じものがないという制約 • EXCLUDE制約は、任意の2行に対して指定し た演算子が真とならない制約 • =演算子のEXCLUDE制約はUNIQUE制約と同 じ
範囲型のEXCLUDE制約 =# select * from level1; level | exp_range -------+-------------
1 | [0,11) 2 | [11,59) 3 | [59,164) ... =# insert into level1 values(100, '[11,12)'); ERROR: 重複キーの値が排除制約 "level1_exp_range_excl" に 違反しています DETAIL: キー (exp_range)=([11,12)) が既存のキー (exp_range) =([11,59)) と競合しています
タイプ レベル 開始 終了 100万 1 0 11 100万 2
11 59 100万 3 59 164 150万 1 0 16 150万 2 16 99 150万 3 89 246
スカラ型と範囲型の組み合わせ -- gistは標準では = を使えない create extension btree_gist; create table
level2 ( exp_type int, level int, exp_range int4range, primary key(exp_type, level), exclude using gist (exp_type with =, exp_range with &&) ); insert into level2 values (100, 1, '[0,11)'), (100, 2, '[11,59)'), ... (150, 1, '[0,16)'), (150, 2, '[16,89)'),
こんなに便利 • 境界を含む、含まないを表現できる • 上限、下限の無い範囲を表現できる • インデックスが効く • EXCLUDE制約で、重複していないことを保証 できる
• 豊富な演算子