Skip to content

Commit

Permalink
pg-introspection additions (#1927)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie authored Jan 31, 2024
2 parents 504414c + 343e147 commit 4d847a5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .changeset/friendly-meals-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"postgraphile": patch
"pg-introspection": patch
---

Excludes table constraints on tables from extensions if configured to not
include extensions.
6 changes: 6 additions & 0 deletions .changeset/loud-carpets-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"postgraphile": patch
"pg-introspection": patch
---

Add pg_am to pg-introspection to enable determining index access method
9 changes: 8 additions & 1 deletion utils/pg-introspection/src/augmentIntrospection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function del<T extends Record<string, any>, TKey extends keyof T>(
) {
for (let i = collection.length - 1; i >= 0; i--) {
const entry = collection[i];
if (toDelete.has(entry[attr])) {
if (entry[attr] != null && toDelete.has(entry[attr])) {
collection.splice(i, 1);
}
}
Expand Down Expand Up @@ -91,6 +91,8 @@ export function augmentIntrospection(
del(extensionProcOids, introspection.procs, "_id");
del(extensionClassOids, introspection.classes, "_id");
del(extensionClassOids, introspection.attributes, "attrelid");
del(extensionClassOids, introspection.constraints, "conrelid");
del(extensionClassOids, introspection.constraints, "confrelid");
del(extensionClassOids, introspection.types, "typrelid");
}

Expand Down Expand Up @@ -297,6 +299,11 @@ export function augmentIntrospection(
entity.getInherited = memo(() =>
introspection.inherits.filter((inh) => inh.inhrelid === entity._id),
);
entity.getAccessMethod = memo(() =>
entity.relam != null
? introspection.am.find((am) => am._id === entity.relam)
: undefined,
);
});
introspection.indexes.forEach((entity) => {
entity._type = "PgIndex";
Expand Down
1 change: 1 addition & 0 deletions utils/pg-introspection/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ declare module "./introspection" {
getTags(): PgSmartTagsDict;
getACL(): AclObject[];
getInherited(): PgInherits[];
getAccessMethod(): PgAm | undefined;
}
interface PgIndex {
_type: "PgIndex";
Expand Down
32 changes: 31 additions & 1 deletion utils/pg-introspection/src/introspection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,25 @@ export interface PgDescription {
description: string;
}

/**
* The catalog pg_am stores information about relation access methods. There is one row for each access method
* supported by the system. Currently, only tables and indexes have access methods. The requirements for table and
* index access methods are discussed in detail in [tableam] and [indexam] respectively.
*/
export interface PgAm {
/** Row identifier */
_id: PgOid;

/** Name of the access method */
amname: PgName | null;

/** OID of a handler function that is responsible for supplying information about the access method */
amhandler: PgOid | null;

/** t = table (including materialized views), i = index. */
amtype: string | null;
}

/**
* This type contains a description of everything we care about in the database.
*/
Expand All @@ -1409,6 +1428,7 @@ export interface Introspection {
ranges: Array<PgRange>;
depends: Array<PgDepend>;
descriptions: Array<PgDescription>;
am: Array<PgAm>;

/**
* Catalogs such as pg_class, pg_attribute, etc have oids; this loopup lets us
Expand Down Expand Up @@ -1448,7 +1468,8 @@ export type PgEntity =
| PgLanguage
| PgRange
| PgDepend
| PgDescription;
| PgDescription
| PgAm;

// We might want this to take options in future, so we've made it a function.
/**
Expand Down Expand Up @@ -1574,6 +1595,12 @@ with
or (classoid = 'pg_catalog.pg_enum'::regclass and objoid in (select enums._id from enums))
or (classoid = 'pg_catalog.pg_extension'::regclass and objoid in (select extensions._id from extensions))
)
),
am as (
select pg_am.oid as _id, *
from pg_catalog.pg_am
where true
)
select json_build_object(
'database',
Expand Down Expand Up @@ -1627,6 +1654,9 @@ select json_build_object(
'descriptions',
(select coalesce((select json_agg(row_to_json(descriptions) order by objoid, classoid, objsubid) from descriptions), '[]'::json)),
'am',
(select coalesce((select json_agg(row_to_json(am) order by amname) from am), '[]'::json)),
'catalog_by_oid',
(
select json_object_agg(oid::text, relname order by relname asc)
Expand Down

0 comments on commit 4d847a5

Please sign in to comment.