Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pg_graphql performance #3204

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,60 @@ CREATE FUNCTION graphql."_internal_resolve"(
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'resolve_wrapper';

-- src/lib.rs:19
-- Is updated every time the schema changes
create sequence if not exists graphql.seq_schema_version as int cycle;

create or replace function graphql.increment_schema_version()
returns event_trigger
security definer
language plpgsql
as $$
begin
perform nextval('graphql.seq_schema_version');
end;
$$;

create or replace function graphql.get_schema_version()
returns int
security definer
language sql
as $$
select last_value from graphql.seq_schema_version;
$$;

-- On DDL event, increment the schema version number
create event trigger graphql_watch_ddl
on ddl_command_end
execute procedure graphql.increment_schema_version();

create event trigger graphql_watch_drop
on sql_drop
execute procedure graphql.increment_schema_version();


-- src/lib.rs:20
create function graphql.comment_directive(comment_ text)
returns jsonb
language sql
immutable
as $$
/*
comment on column public.account.name is '@graphql.name: myField'
*/
select
coalesce(
(
regexp_match(
comment_,
'@graphql\((.+?)\)'
)
)[1]::jsonb,
jsonb_build_object()
)
$$;


-- src/lib.rs:22
-- requires:
-- resolve
Expand Down Expand Up @@ -50,28 +104,6 @@ end;
$$;


-- src/lib.rs:20
create function graphql.comment_directive(comment_ text)
returns jsonb
language sql
immutable
as $$
/*
comment on column public.account.name is '@graphql.name: myField'
*/
select
coalesce(
(
regexp_match(
comment_,
'@graphql\((.+?)\)'
)
)[1]::jsonb,
jsonb_build_object()
)
$$;


-- src/lib.rs:21
create or replace function graphql.exception(message text)
returns text
Expand All @@ -82,35 +114,3 @@ begin
end;
$$;


-- src/lib.rs:19
-- Is updated every time the schema changes
create sequence if not exists graphql.seq_schema_version as int cycle;

create or replace function graphql.increment_schema_version()
returns event_trigger
security definer
language plpgsql
as $$
begin
perform nextval('graphql.seq_schema_version');
end;
$$;

create or replace function graphql.get_schema_version()
returns int
security definer
language sql
as $$
select last_value from graphql.seq_schema_version;
$$;

-- On DDL event, increment the schema version number
create event trigger graphql_watch_ddl
on ddl_command_end
execute procedure graphql.increment_schema_version();

create event trigger graphql_watch_drop
on sql_drop
execute procedure graphql.increment_schema_version();

Binary file not shown.
15 changes: 12 additions & 3 deletions packages/twenty-postgres/macos/arm/build-postgres-macos-arm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ EOF
echo_header $BLUE " DATABASE SETUP"

PG_MAIN_VERSION=15
PG_GRAPHQL_VERSION=1.3.0
CARGO_PGRX_VERSION=0.9.8
PG_GRAPHQL_VERSION=1.4.2
CARGO_PGRX_VERSION=0.10.2

current_directory=$(pwd)
script_directory="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"

# Install PostgresSQL
echo_header $GREEN "Step [1/4]: Installing PostgreSQL..."
Expand Down Expand Up @@ -85,9 +86,17 @@ unzip pg_graphql-$PG_GRAPHQL_VERSION.zip
[[ ":$PATH:" != *":/opt/homebrew/opt/postgresql@$PG_MAIN_VERSION/bin:"* ]] && PATH="/opt/homebrew/opt/postgresql@$PG_MAIN_VERSION/bin:${PATH}"

cd "pg_graphql-$PG_GRAPHQL_VERSION"

# Apply patches to pg_graphql files
echo "Applying patches to pg_graphql files..."
for patch_file in "$script_directory/../../patches/pg_graphql/"*.patch; do
echo "Applying patch: $patch_file"
patch -p1 < "$patch_file"
done

cargo pgrx install --release --pg-config /opt/homebrew/opt/postgresql@$PG_MAIN_VERSION/bin/pg_config

# # Clean up the temporary directory
# Clean up the temporary directory
echo "Cleaning up..."
cd "$current_directory"
rm -rf "$temp_dir"
Expand Down
15 changes: 12 additions & 3 deletions packages/twenty-postgres/macos/intel/build-postgres-macos-intel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ EOF
echo_header $BLUE " DATABASE SETUP"

PG_MAIN_VERSION=15
PG_GRAPHQL_VERSION=1.3.0
CARGO_PGRX_VERSION=0.9.8
PG_GRAPHQL_VERSION=1.4.2
CARGO_PGRX_VERSION=0.10.2

current_directory=$(pwd)
script_directory="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"

# Install PostgresSQL
echo_header $GREEN "Step [1/4]: Installing PostgreSQL..."
Expand Down Expand Up @@ -85,9 +86,17 @@ unzip pg_graphql-$PG_GRAPHQL_VERSION.zip
[[ ":$PATH:" != *":/usr/local/opt/postgresql@$PG_MAIN_VERSION/bin:"* ]] && PATH="/usr/local/opt/postgresql@$PG_MAIN_VERSION/bin:${PATH}"

cd "pg_graphql-$PG_GRAPHQL_VERSION"

# Apply patches to pg_graphql files
echo "Applying patches to pg_graphql files..."
for patch_file in "$script_directory/../../patches/pg_graphql/"*.patch; do
echo "Applying patch: $patch_file"
patch -p1 < "$patch_file"
done

cargo pgrx install --release --pg-config /usr/local/opt/postgresql@$PG_MAIN_VERSION/bin/pg_config

# # Clean up the temporary directory
# Clean up the temporary directory
echo "Cleaning up..."
cd "$current_directory"
rm -rf "$temp_dir"
Expand Down
28 changes: 28 additions & 0 deletions packages/twenty-postgres/patches/pg_graphql/0001-performance.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
diff --git a/sql/load_sql_context.sql b/sql/load_sql_context.sql
index 565e4e3..40cd99e 100644
--- a/sql/load_sql_context.sql
+++ b/sql/load_sql_context.sql
@@ -95,6 +95,8 @@ select
pg_type pt
left join pg_class tabs
on pt.typrelid = tabs.oid
+ join search_path_oids spo
+ on pt.typnamespace = spo.schema_oid or pt.typnamespace = 'pg_catalog'::regnamespace::oid
),
jsonb_build_object()
),
@@ -111,6 +113,8 @@ select
pg_type pt
join pg_class tabs
on pt.typrelid = tabs.oid
+ join search_path_oids spo
+ on pt.typnamespace = spo.schema_oid or pt.typnamespace = 'pg_catalog'::regnamespace::oid
where
pt.typcategory = 'C'
and tabs.relkind = 'c'
@@ -420,4 +424,4 @@ select
jsonb_build_array()
)

- )
+ );
Loading