Skip to content

Commit 95a6e49

Browse files
jnasbyupgradeclaude
andcommitted
Resolve object_reference extension row once in _is_own_object()
Previously ran three separate lookups against pg_extension per call (one per OR-branch). Resolve oid/extnamespace once via a FROM-clause subquery and reuse them across all three branches. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 1aaa37c commit 95a6e49

3 files changed

Lines changed: 55 additions & 63 deletions

File tree

‎sql/object_reference--0.1.0--stable.sql‎

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -140,35 +140,31 @@ SELECT __object_reference.create_function(
140140
$args$
141141
, 'boolean LANGUAGE sql STABLE'
142142
, $body$
143-
SELECT EXISTS(
144-
SELECT 1
145-
FROM pg_catalog.pg_depend d
146-
WHERE d.classid = _is_own_object.classid
147-
AND d.objid = _is_own_object.objid
148-
AND d.deptype = 'e'
149-
AND d.refclassid = 'pg_catalog.pg_extension'::regclass
150-
AND d.refobjid = (SELECT oid FROM pg_catalog.pg_extension WHERE extname = 'object_reference')
151-
)
152-
/*
153-
* The extension's own declared schema (object_reference) is a special
154-
* case: CREATE EXTENSION records the EXTENSION as depending on it (a plain
155-
* DEPENDENCY_NORMAL row, extension -> schema), not the schema as an 'e'
156-
* member of the extension the way every other object it creates is -- so
157-
* it never matches the pg_depend check above.
158-
*/
159-
OR (
160-
_is_own_object.classid = 'pg_catalog.pg_namespace'::regclass
161-
AND _is_own_object.objid = (SELECT extnamespace FROM pg_catalog.pg_extension WHERE extname = 'object_reference')
162-
)
163-
/*
164-
* The extension's own pg_extension row is also its own special case: it
165-
* isn't a member of itself (no 'e' row with itself as both member and
166-
* owner), so treat it as one explicitly.
167-
*/
168-
OR (
169-
_is_own_object.classid = 'pg_catalog.pg_extension'::regclass
170-
AND _is_own_object.objid = (SELECT oid FROM pg_catalog.pg_extension WHERE extname = 'object_reference')
171-
)
143+
SELECT
144+
EXISTS(
145+
SELECT 1
146+
FROM pg_catalog.pg_depend d
147+
WHERE d.classid = _is_own_object.classid
148+
AND d.objid = _is_own_object.objid
149+
AND d.deptype = 'e'
150+
AND d.refclassid = 'pg_catalog.pg_extension'::regclass
151+
AND d.refobjid = e.oid
152+
)
153+
/*
154+
* The extension's own declared schema (object_reference) is a special
155+
* case: CREATE EXTENSION records the EXTENSION as depending on it (a
156+
* plain DEPENDENCY_NORMAL row, extension -> schema), not the schema as
157+
* an 'e' member of the extension the way every other object it creates
158+
* is -- so it never matches the pg_depend check above.
159+
*/
160+
OR (_is_own_object.classid = 'pg_catalog.pg_namespace'::regclass AND _is_own_object.objid = e.extnamespace)
161+
/*
162+
* The extension's own pg_extension row is also its own special case: it
163+
* isn't a member of itself (no 'e' row with itself as both member and
164+
* owner), so treat it as one explicitly.
165+
*/
166+
OR (_is_own_object.classid = 'pg_catalog.pg_extension'::regclass AND _is_own_object.objid = e.oid)
167+
FROM (SELECT oid, extnamespace FROM pg_catalog.pg_extension WHERE extname = 'object_reference') e
172168
$body$
173169
, 'Is the object a member of the object_reference extension itself? (pg_depend deptype = e membership, not just co-installation.)'
174170
);

‎sql/object_reference.sql‎

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -169,35 +169,31 @@ SELECT __object_reference.create_function(
169169
$args$
170170
, 'boolean LANGUAGE sql STABLE'
171171
, $body$
172-
SELECT EXISTS(
173-
SELECT 1
174-
FROM pg_catalog.pg_depend d
175-
WHERE d.classid = _is_own_object.classid
176-
AND d.objid = _is_own_object.objid
177-
AND d.deptype = 'e'
178-
AND d.refclassid = 'pg_catalog.pg_extension'::regclass
179-
AND d.refobjid = (SELECT oid FROM pg_catalog.pg_extension WHERE extname = 'object_reference')
180-
)
181-
/*
182-
* The extension's own declared schema (object_reference) is a special
183-
* case: CREATE EXTENSION records the EXTENSION as depending on it (a plain
184-
* DEPENDENCY_NORMAL row, extension -> schema), not the schema as an 'e'
185-
* member of the extension the way every other object it creates is -- so
186-
* it never matches the pg_depend check above.
187-
*/
188-
OR (
189-
_is_own_object.classid = 'pg_catalog.pg_namespace'::regclass
190-
AND _is_own_object.objid = (SELECT extnamespace FROM pg_catalog.pg_extension WHERE extname = 'object_reference')
191-
)
192-
/*
193-
* The extension's own pg_extension row is also its own special case: it
194-
* isn't a member of itself (no 'e' row with itself as both member and
195-
* owner), so treat it as one explicitly.
196-
*/
197-
OR (
198-
_is_own_object.classid = 'pg_catalog.pg_extension'::regclass
199-
AND _is_own_object.objid = (SELECT oid FROM pg_catalog.pg_extension WHERE extname = 'object_reference')
200-
)
172+
SELECT
173+
EXISTS(
174+
SELECT 1
175+
FROM pg_catalog.pg_depend d
176+
WHERE d.classid = _is_own_object.classid
177+
AND d.objid = _is_own_object.objid
178+
AND d.deptype = 'e'
179+
AND d.refclassid = 'pg_catalog.pg_extension'::regclass
180+
AND d.refobjid = e.oid
181+
)
182+
/*
183+
* The extension's own declared schema (object_reference) is a special
184+
* case: CREATE EXTENSION records the EXTENSION as depending on it (a
185+
* plain DEPENDENCY_NORMAL row, extension -> schema), not the schema as
186+
* an 'e' member of the extension the way every other object it creates
187+
* is -- so it never matches the pg_depend check above.
188+
*/
189+
OR (_is_own_object.classid = 'pg_catalog.pg_namespace'::regclass AND _is_own_object.objid = e.extnamespace)
190+
/*
191+
* The extension's own pg_extension row is also its own special case: it
192+
* isn't a member of itself (no 'e' row with itself as both member and
193+
* owner), so treat it as one explicitly.
194+
*/
195+
OR (_is_own_object.classid = 'pg_catalog.pg_extension'::regclass AND _is_own_object.objid = e.oid)
196+
FROM (SELECT oid, extnamespace FROM pg_catalog.pg_extension WHERE extname = 'object_reference') e
201197
$body$
202198
, 'Is the object a member of the object_reference extension itself? (pg_depend deptype = e membership, not just co-installation.)'
203199
);

‎test/build/expected/build.out‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ This extension must be loaded via CREATE EXTENSION object_reference;
33
You really, REALLY do NOT want to try and load this via psql!!!
44

55

6-
psql:test/temp_load.not_sql:217: WARNING: I promise you will be sorry if you try to use this as anything other than an extension!
6+
psql:test/temp_load.not_sql:213: WARNING: I promise you will be sorry if you try to use this as anything other than an extension!
77

8-
psql:test/temp_load.not_sql:218: WARNING: I promise you will be sorry if you try to use this as anything other than an extension!
8+
psql:test/temp_load.not_sql:214: WARNING: I promise you will be sorry if you try to use this as anything other than an extension!
99

1010

1111

1212

1313

1414

15-
psql:test/temp_load.not_sql:466: WARNING: I promise you will be sorry if you try to use this as anything other than an extension!
15+
psql:test/temp_load.not_sql:462: WARNING: I promise you will be sorry if you try to use this as anything other than an extension!
1616

1717

1818

@@ -22,9 +22,9 @@ psql:test/temp_load.not_sql:466: WARNING: I promise you will be sorry if you tr
2222

2323

2424

25-
psql:test/temp_load.not_sql:578: WARNING: I promise you will be sorry if you try to use this as anything other than an extension!
25+
psql:test/temp_load.not_sql:574: WARNING: I promise you will be sorry if you try to use this as anything other than an extension!
2626

27-
psql:test/temp_load.not_sql:585: WARNING: I promise you will be sorry if you try to use this as anything other than an extension!
27+
psql:test/temp_load.not_sql:581: WARNING: I promise you will be sorry if you try to use this as anything other than an extension!
2828

2929

3030

0 commit comments

Comments
 (0)