-
Notifications
You must be signed in to change notification settings - Fork 23
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
Add Postgres.js #55
Open
porsager
wants to merge
4
commits into
edgedb:master
Choose a base branch
from
porsager:postgres-js
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Postgres.js #55
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
/* eslint max-len: 0 */ | ||
|
||
const postgres = require('postgres') | ||
|
||
module.exports = function(options) { | ||
const concurrency = options.max | ||
const INSERT_PREFIX = 'insert_test__' | ||
const sql = postgres({ | ||
fetch_types: false, | ||
user: 'postgres_bench', | ||
host: 'localhost', | ||
database: 'postgres_bench', | ||
password: 'edgedbbenchmark', | ||
port: 15432, | ||
...(options || {}) | ||
}) | ||
|
||
const queries = { | ||
get_user, | ||
get_person, | ||
get_movie, | ||
update_movie, | ||
insert_user, | ||
insert_movie, | ||
insert_movie_plus | ||
} | ||
|
||
const api = { | ||
benchQuery: (name, id) => queries[name](id), | ||
connect: () => sql`select 1`, | ||
getConnection: () => api, | ||
getIDs, | ||
setup, | ||
cleanup | ||
} | ||
|
||
return api | ||
|
||
async function get_user(id) { | ||
const [user] = await sql` | ||
select | ||
u.id, | ||
u.name, | ||
u.image, | ||
json_agg(q) as latest_reviews | ||
from users u, lateral ( | ||
select | ||
r.id as review_id, | ||
r.body as review_body, | ||
r.rating as review_rating, | ||
json_build_object( | ||
'id', m.id, | ||
'image', m.image, | ||
'title', m.title, | ||
'avg_rating', m.avg_rating::float | ||
) as movie | ||
from reviews as r | ||
inner join movies as m on (r.movie_id = m.id) | ||
where r.author_id = u.id | ||
order by r.creation_time desc | ||
limit 10 | ||
) as q | ||
where u.id = ${ id } | ||
group by u.id | ||
` | ||
|
||
return user | ||
} | ||
|
||
async function get_movie(id) { | ||
const [x] = await sql` | ||
select | ||
m.id, | ||
m.image, | ||
m.title, | ||
m.year, | ||
m.description, | ||
m.avg_rating::float, | ||
( | ||
select | ||
json_agg(q) | ||
from ( | ||
select p.id, p.full_name, p.image | ||
from directors | ||
join persons p on (directors.person_id = p.id) | ||
where directors.movie_id = m.id | ||
order by directors.list_order nulls last, p.last_name | ||
) q | ||
) directors, | ||
( | ||
select | ||
json_agg(q) | ||
from ( | ||
select p.id, p.full_name, p.image | ||
from actors | ||
join persons p on actors.person_id = p.id | ||
where actors.movie_id = m.id | ||
order by actors.list_order nulls last, p.last_name | ||
) q | ||
) actors, | ||
( | ||
select | ||
json_agg(q) | ||
from ( | ||
select r.id, r.body, r.rating, | ||
json_build_object( | ||
'id', a.id, | ||
'name', a.name, | ||
'image', a.image | ||
) author | ||
from reviews r | ||
join users a on r.author_id = a.id | ||
where r.movie_id = m.id | ||
order by r.creation_time desc | ||
) q | ||
) reviews | ||
from movies m | ||
where id = ${ id } | ||
` | ||
|
||
return x | ||
} | ||
|
||
async function insert_movie(val) { | ||
const num = Math.floor(Math.random() * 1000000) | ||
const [user] = await sql` | ||
with m as ( | ||
insert into movies (title, image, description, year) | ||
values ( | ||
${ val.prefix + num }, | ||
${ val.prefix + 'image' + num + '.jpeg' }, | ||
${ val.prefix + 'description' + num }, | ||
${ num } | ||
) | ||
returning id, title, image, description, year | ||
), d as ( | ||
select | ||
id, | ||
p.full_name, | ||
image | ||
from persons p | ||
where id = ${ val.people[0] } | ||
), c as ( | ||
select | ||
id, | ||
p.full_name, | ||
image | ||
from persons p | ||
where id in (${ val.people[1] }, ${ val.people[1] }, ${ val.people[1] }) | ||
), dl as ( | ||
insert into directors (person_id, movie_id) | ||
(select d.id, m.id from m, d) | ||
), cl as ( | ||
insert into actors (person_id, movie_id) | ||
(select c.id, m.id from m, c) | ||
) | ||
select | ||
m.id, | ||
m.image, | ||
m.title, | ||
m.year, | ||
m.description, | ||
( | ||
select | ||
json_agg(q) | ||
from ( | ||
select id, full_name, image | ||
from d | ||
) q | ||
) directors, | ||
( | ||
select | ||
json_agg(q) | ||
from ( | ||
select id, full_name, image | ||
from c | ||
) q | ||
) actors | ||
from m | ||
` | ||
|
||
return user | ||
} | ||
|
||
async function update_movie() { | ||
throw new Error('not implemented') | ||
} | ||
|
||
async function insert_user() { | ||
throw new Error('not implemented') | ||
} | ||
|
||
async function get_person() { | ||
throw new Error('not implemented') | ||
} | ||
|
||
async function insert_movie_plus() { | ||
throw new Error('not implemented') | ||
} | ||
|
||
async function getIDs() { | ||
const ids = await Promise.all([ | ||
sql`select id from users order by random()`, | ||
sql`select id from persons order by random()`, | ||
sql`select id from movies order by random()` | ||
]) | ||
|
||
const people = ids[1].map(x => x.id) | ||
|
||
return { | ||
get_user: ids[0].map(x => x.id), | ||
get_person: people, | ||
get_movie: ids[2].map(x => x.id), | ||
update_movie: ids[2].map(x => x.id), | ||
insert_user: Array(concurrency).fill(INSERT_PREFIX), | ||
insert_movie: Array(concurrency).fill({ prefix: INSERT_PREFIX, people: people.slice(0, 4) }), | ||
insert_movie_plus: Array(concurrency).fill(INSERT_PREFIX) | ||
} | ||
} | ||
|
||
async function setup(query) { | ||
return query === 'update_movie' | ||
? sql`update movies set title = split_part(movies.title, '---', 1) where movies.title like '%---%'` | ||
: query === 'insert_user' | ||
? sql`delete from users where users.name like ${ INSERT_PREFIX + '%' }` | ||
: query === 'insert_movie' || query === 'insert_movie_plus' && Promise.all([ | ||
sql`delete from directors as d using movies as m where d.movie_id = m.id and m.image like ${ INSERT_PREFIX + '%' }`, | ||
sql`delete from actors as a using movies as m where a.movie_id = m.id and m.image like ${ INSERT_PREFIX + '%' }`, | ||
sql`delete from movies where image like ${ INSERT_PREFIX + '%' }`, | ||
sql`delete from persons where image like ${ INSERT_PREFIX + '%' }` | ||
]) | ||
} | ||
|
||
async function cleanup(query) { | ||
if (['update_movie', 'insert_user', 'insert_movie', 'insert_movie_plus'].indexOf(query) >= 0) | ||
return setup(query) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other implementations aren't using in-database JSON encoding, so this wouldn't be a fair comparison of binding performance. See also #48 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is postgres.js capable of decoding arrays and tuples? If so, I'd use the asyncpg queries for fairness.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@elprans it does arrays but not record tuples. I did do a query assembling things in the client just like the go example, but it didn't do any performance difference (i actually think it was a bit faster). In my opinion I think using json in the query is more fair, since PostgreSQL can do so much more - so if you want to honestly compare eg EdgeDB queries to PostgreSQL - choose the best from both worlds. I can change it to match the go query or asyncpg, but I think it would make more sense to maybe change them to use this query instead? I might be biased since I want to stay as close to the "native" queries as possible, which I guess is also part of your reasons for making EdgeDB?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue is that JSON strips rich type information, e.g. instead of a proper
Date
object for atimestamp
column, you get a string, etc, which is significantly less useful for application code. This isn't entirely obvious here, because the current benchmark code just serializes things back to JSON, but it's an important aspect nonetheless.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can add a variation of each query that returns JSON directly, if supported. In fact, EdgeDB explicitly supports database-side JSON encoding of results of arbitrary queries.