Slide 1

Slide 1 text

第51回 PostgreSQLアンカンファレンス@オンライン Yasuo Honda @yahonda extensionとschema

Slide 2

Slide 2 text

● Yasuo Honda @yahonda ○ Rails committer ○ 第50回 PostgreSQLアンカンファレンス 以来5回目の参加です ■ PostgreSQL 15とRailsと - Speaker Deck ■ 遅延可能な一意性制約 - Speaker Deck ■ pg_stat_statementsで inの数が違うSQLをまとめて ほしい - Speaker Deck ■ NOT VALIDな検査制約 / check constraint that is not valid - Speaker Deck 自己紹介

Slide 3

Slide 3 text

● NOT VALIDな検査制約 / check constraint that is not valid - Speaker Deck ● Rails ○ https://github.com/rails/rails/pull/54286 ● PostgreSQL ○ https://www.postgresql.org/message-id/202501141746.ztgt jtjcehr3%40alvherre.pgsql 第50回からのアップデート

Slide 4

Slide 4 text

● Ruby on Railsのmigrationでextensionを作成、削除するDSL ○ enable_extension(“schema_name.extension_name”) ■ Rails 7.1 https://github.com/rails/rails/pull/46894 以降で dot notationな引数に取れるようになった ○ disable_extension(“schema_name.extension_name”) ■ extensionが削除されていなかった 背景

Slide 5

Slide 5 text

● enable_extension(“myschema.hstore”) ○ test=# CREATE EXTENSION IF NOT EXISTS "hstore" SCHEMA myschema; ○ 成功する ● disable_extension(“myschema.hstore”) ○ test=# DROP EXTENSION IF EXISTS "myschema.hstore"; ○ NOTICE: extension "myschema.hstore" does not exist, skipping ○ エラーになっていた: 発行されていた SQL

Slide 6

Slide 6 text

● create_extension ○ そのまま ● disable_extension(“myschema.hstore”) ○ 引数にdotを含む場合に、dotの後半のみをSQLに渡す ○ https://github.com/rails/rails/pull/52452 ○ test=# DROP EXTENSION IF EXISTS "hstore"; ● 様々な疑問が起きる 修正された SQL

Slide 7

Slide 7 text

● dot notationが利用できない ○ test=# CREATE EXTENSION IF NOT EXISTS "myschema.hstore"; ○ ERROR: extension "myschema.hstore" is not available ○ DETAIL: Could not open extension control file "/usr/share/postgresql/16/extension/myschema.hstore.co ntrol": No such file or directory. ○ HINT: The extension must first be installed on the system where PostgreSQL is running. 他のオブジェクトとの違い : create時

Slide 8

Slide 8 text

● そのデータベース内に作成されていたら複数作成できない ○ スキーマ名に関わらず ○ test=# CREATE EXTENSION IF NOT EXISTS "hstore"; ○ CREATE EXTENSION ○ test=# CREATE EXTENSION IF NOT EXISTS "hstore" SCHEMA myschema; ○ NOTICE: extension "hstore" already exists, skipping ○ CREATE EXTENSION 他のオブジェクトとの違い : create時

Slide 9

Slide 9 text

● SCHEMA句も利用できない ○ test=# DROP EXTENSION IF EXISTS "hstore" SCHEMA myschema; ○ ERROR: syntax error at or near "SCHEMA" ● dot notationも利用できない ○ test=# DROP EXTENSION IF EXISTS "myschema.hstore"; ○ NOTICE: extension "myschema.hstore" does not exist, skipping 他のオブジェクトとの違い : drop時

Slide 10

Slide 10 text

● https://www.postgresql.org/docs/current/sql-createextension .html ● > The name of the schema in which to install the extension's objects, given that the extension allows its contents to be relocated. ● https://www.postgresql.jp/document/16/html/sql-createexten sion.html ● > 拡張の内容を再配置させることができる場合に、拡張のオブジェクトを インストールするスキーマの名前です。 Documentation (下線部は発表者による )

Slide 11

Slide 11 text

● https://www.postgresql.org/docs/current/catalog-pg-extensio n.html ○ Note that unlike most catalogs with a “namespace” column, extnamespace is not meant to imply that the extension belongs to that schema. Extension names are never schema-qualified. Rather, extnamespace indicates the schema that contains most or all of the extension's objects. If extrelocatable is true, then this schema must in fact contain all schema-qualifiable objects belonging to the extension. Relocated / 再配置 (下線は発表者 )

Slide 12

Slide 12 text

● SELECT extname, extnamespace::regnamespace::text AS schema_name, extrelocatable FROM pg_extension; Relocated / 再配置

Slide 13

Slide 13 text

● create schema another_schema; ● alter extension hstore set schema another_schema; ● https://www.postgresql.org/docs/current/sql-alterextension.h tml Relocated / 再配置

Slide 14

Slide 14 text

● PostgreSQL 9.1から導入 ○ https://www.postgresql.org/docs/9.1/sql-createextension. html ● 関連commit(s) ○ https://github.com/postgres/postgres/commit/d9572c4e3 b4 ○ https://github.com/postgres/postgres/commit/1214749901f extensionが導入されたバージョン / commit

Slide 15

Slide 15 text

● このユースケースはどこから来たのか ● extensionがschemaに属さないオブジェクトならこのような複雑な仕組 みにしなくてもいいのではないか ● create_extensionにdotの引数を取れるようにした(mergeした)のはあ まりいいAPIデザインではなかったのではないかという反省 ○ https://devcenter.heroku.com/changelog-items/2446 という ユースケースがあったので難しいが... 疑問と反省

Slide 16

Slide 16 text

おわり