Skip to content

Commit dcba8e0

Browse files
cbaker6acinader
andauthored
Case insensitive username and email indexing and query planning for Postgres (#6506)
* Update .travis.yml testing error to see what happens... * Update .travis.yml Attempting to resolve postgres in CL by installing postgis via sudo instead of through apt/packages * Update .travis.yml * Update .travis.yml * Update .travis.yml Removed extra lines of postgres that were under "services" and "addons". I believe the "postgresql" line under "services" was installing the default of 9.6 and "addons" was installing postgres 11. My guess is the fail was occurring due to 9.6 being called sometimes and it never had postgis installed. If this is true, the solution is to only install one version of postgres, which is version 11 with postgis 2.5. * Adding test case for caseInsensitive Adding test case for verifying indexing for caseInsensitive * Implementing ensureIndex * Updated PostgresStorageAdapter calls to ST_DistanceSphere. Note this has a minimum requirement of postgis 2.2. Documented the change in the readme. This is address #6441 * updated postgres sections of contributions with newer postgres info. Also switched postgis image it points to as the other one hasn't been updated in over a year. * more info about postgres * added necessary password for postgres docker * updated wording in contributions * removed reference to MacJr environment var when starting postgres in contributions. The official image automatically creates a user named 'postgres', but it does require a password, which the command sets to 'postgres' * added more time to docker sleep/wait to enter postgis commands. This will always take a few seconds because the db is installing from scratch everytime. If postgres/postgis images aren't already downloaded locally, it will take even longer. Worst case, if the command times out on first run. Stop and remove the parse-postgres container and run the command again, 20 seconds should be enough wait time then * latest changes * initial fix, need to test * fixed lint * Adding test case for caseInsensitive Adding test case for verifying indexing for caseInsensitive * Implementing ensureIndex * Updated PostgresStorageAdapter calls to ST_DistanceSphere. Note this has a minimum requirement of postgis 2.2. Documented the change in the readme. This is address #6441 * updated postgres sections of contributions with newer postgres info. Also switched postgis image it points to as the other one hasn't been updated in over a year. * more info about postgres * added necessary password for postgres docker * updated wording in contributions * removed reference to MacJr environment var when starting postgres in contributions. The official image automatically creates a user named 'postgres', but it does require a password, which the command sets to 'postgres' * added more time to docker sleep/wait to enter postgis commands. This will always take a few seconds because the db is installing from scratch everytime. If postgres/postgis images aren't already downloaded locally, it will take even longer. Worst case, if the command times out on first run. Stop and remove the parse-postgres container and run the command again, 20 seconds should be enough wait time then * latest changes * initial fix, need to test * fixed lint * Adds caseInsensitive constraints to database, but doesn't pass regular tests. I believe this is because ensureIndex in the Postgres adapter is returning wrong. Also, some issues with the caseInsensitive test case * this version addes the indexes, but something still wrong with the ensureIndex method in adapter * removed code from suggestions * fixed lint * fixed PostgresAdapter test case * small bug in test case * reverted back to main branch package.json and lock file * fixed docker command in Contribute file * added ability to explain the find method * triggering another build * added ability to choose to 'analyze' a query which actually executes (this can be bad when looking at a query plan for Insert, Delete, etc.) the query or to just setup the query plan (default, previous versions defaulted to 'analyze'). Alse added some comparsons on sequential vs index searches for postgres * made sure to check that search actually returns 1 result. Removed prep time comparison between searches as this seemed to be variable * added test cases using find and case insensitivity on fields other than username and password. Also added explain to aggregate method * fixing issue where query in aggregate replaced the map method incorrectly * reverted back to mapping for aggregate method to make sure it's the issue * switched back to caseInsensitive check for email and username as it was causing issues * fixed aggregate method using explain * made query plain results more flexible/reusable. Got rid of droptables as 'beforeEach' already handles this * updated CONTRIBUTING doc to use netrecon as default username for postgres (similar to old style). Note that the official postgres docker image for postgres requires POSTGRES_PASSWORD to be set in order to use the image * left postgis at 2.5 in the contributing document as this is the last version to be backwards compatibile with older versions of parse server * updating docker command for postgres Co-authored-by: Arthur Cinader <[email protected]>
1 parent a8a367e commit dcba8e0

File tree

5 files changed

+303
-24
lines changed

5 files changed

+303
-24
lines changed

CONTRIBUTING.md

+17-7
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ If your pull request introduces a change that may affect the storage or retrieva
6161
* Run the tests against the postgres database with `PARSE_SERVER_TEST_DB=postgres PARSE_SERVER_TEST_DATABASE_URI=postgres://postgres:password@localhost:5432/parse_server_postgres_adapter_test_database npm run testonly`. You'll need to have postgres running on your machine and setup [appropriately](https://github.com/parse-community/parse-server/blob/master/.travis.yml#L43) or use [`Docker`](#run-a-parse-postgres-with-docker).
6262
* The Postgres adapter has a special debugger that traces all the sql commands. You can enable it with setting the environment variable `PARSE_SERVER_LOG_LEVEL=debug`
6363
* If your feature is intended to only work with MongoDB, you should disable PostgreSQL-specific tests with:
64-
64+
6565
- `describe_only_db('mongo')` // will create a `describe` that runs only on mongoDB
6666
- `it_only_db('mongo')` // will make a test that only runs on mongo
6767
- `it_exclude_dbs(['postgres'])` // will make a test that runs against all DB's but postgres
@@ -71,22 +71,32 @@ If your pull request introduces a change that may affect the storage or retrieva
7171
- `it_only_db('postgres')` // will make a test that only runs on postgres
7272
- `it_exclude_dbs(['mongo'])` // will make a test that runs against all DB's but mongo
7373

74-
#### Run a Parse Postgres with Docker
74+
#### Run Postgres setup for Parse with Docker
7575

76-
To launch the compatible Postgres instance, copy and paste the following line into your shell:
76+
[PostGIS images (select one with v2.2 or higher) on docker dashboard](https://hub.docker.com/r/postgis/postgis) is based off of the official [postgres](https://registry.hub.docker.com/_/postgres/) image and will work out-of-the-box (as long as you create a user with the necessary extensions for each of your Parse databases; see below). To launch the compatible Postgres instance, copy and paste the following line into your shell:
7777

78-
```sh
79-
docker run -d --name parse-postgres -p 5432:5432 -e POSTGRES_USER=$USER --rm mdillon/postgis:11-alpine && sleep 5 && docker exec -it parse-postgres psql -U $USER -c 'create database parse_server_postgres_adapter_test_database;' && docker exec -it parse-postgres psql -U $USER -c 'CREATE EXTENSION postgis;' -d parse_server_postgres_adapter_test_database && docker exec -it parse-postgres psql -U $USER -c 'CREATE EXTENSION postgis_topology;' -d parse_server_postgres_adapter_test_database
78+
```
79+
docker run -d --name parse-postgres -p 5432:5432 -e POSTGRES_PASSWORD=password --rm postgis/postgis:11-3.0-alpine && sleep 20 && docker exec -it parse-postgres psql -U postgres -c 'CREATE DATABASE parse_server_postgres_adapter_test_database;' && docker exec -it parse-postgres psql -U postgres -c 'CREATE EXTENSION postgis;' -d parse_server_postgres_adapter_test_database && docker exec -it parse-postgres psql -U postgres -c 'CREATE EXTENSION postgis_topology;' -d parse_server_postgres_adapter_test_database
8080
```
8181
To stop the Postgres instance:
8282

83-
```sh
83+
```
8484
docker stop parse-postgres
8585
```
8686

87+
You can also use the [postgis/postgis:11-2.5-alpine](https://hub.docker.com/r/postgis/postgis) image in a Dockerfile and copy this [script](https://github.com/parse-community/parse-server/blob/master/scripts/before_script_postgres.sh) to the image by adding the following lines:
88+
89+
```
90+
#Install additional scripts. These are run in abc order during initial start
91+
COPY ./scripts/setup-dbs.sh /docker-entrypoint-initdb.d/setup-dbs.sh
92+
RUN chmod +x /docker-entrypoint-initdb.d/setup-dbs.sh
93+
```
94+
95+
Note that the script above will ONLY be executed during initialization of the container with no data in the database, see the official [Postgres image](https://hub.docker.com/_/postgres) for details. If you want to use the script to run again be sure there is no data in the /var/lib/postgresql/data of the container.
96+
8797
### Generate Parse Server Config Definition
8898

89-
If you want to make changes to [Parse Server Configuration][config] add the desired configuration to [src/Options/index.js][config-index] and run `npm run definitions`. This will output [src/Options/Definitions.js][config-def] and [src/Options/docs.js][config-docs].
99+
If you want to make changes to [Parse Server Configuration][config] add the desired configuration to [src/Options/index.js][config-index] and run `npm run definitions`. This will output [src/Options/Definitions.js][config-def] and [src/Options/docs.js][config-docs].
90100

91101
To view docs run `npm run docs` and check the `/out` directory.
92102

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ The fastest and easiest way to get started is to run MongoDB and Parse Server lo
7979
Before you start make sure you have installed:
8080

8181
- [NodeJS](https://www.npmjs.com/) that includes `npm`
82-
- [MongoDB](https://www.mongodb.com/) or [PostgreSQL](https://www.postgresql.org/)
82+
- [MongoDB](https://www.mongodb.com/) or [PostgreSQL](https://www.postgresql.org/)(with [PostGIS](https://postgis.net) 2.2.0 or higher)
8383
- Optionally [Docker](https://www.docker.com/)
8484

8585
### Locally

package-lock.json

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/PostgresStorageAdapter.spec.js

+201
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ describe_only_db('postgres')('PostgresStorageAdapter', () => {
8686
expect(columns).toContain('columnA');
8787
expect(columns).toContain('columnB');
8888
expect(columns).toContain('columnC');
89+
8990
done();
9091
})
9192
.catch(error => done.fail(error));
@@ -145,6 +146,206 @@ describe_only_db('postgres')('PostgresStorageAdapter', () => {
145146
undefined
146147
);
147148
});
149+
150+
it('should use index for caseInsensitive query using Postgres', async () => {
151+
const tableName = '_User';
152+
const schema = {
153+
fields: {
154+
objectId: { type: 'String' },
155+
username: { type: 'String' },
156+
email: { type: 'String' },
157+
},
158+
};
159+
const client = adapter._client;
160+
await adapter.createTable(tableName, schema);
161+
await client.none(
162+
'INSERT INTO $1:name ($2:name, $3:name) VALUES ($4, $5)',
163+
[tableName, 'objectId', 'username', 'Bugs', 'Bunny']
164+
);
165+
//Postgres won't take advantage of the index until it has a lot of records because sequential is faster for small db's
166+
await client.none(
167+
'INSERT INTO $1:name ($2:name, $3:name) SELECT MD5(random()::text), MD5(random()::text) FROM generate_series(1,5000)',
168+
[tableName, 'objectId', 'username']
169+
);
170+
const caseInsensitiveData = 'bugs';
171+
const originalQuery =
172+
'SELECT * FROM $1:name WHERE lower($2:name)=lower($3)';
173+
const analyzedExplainQuery = adapter.createExplainableQuery(
174+
originalQuery,
175+
true
176+
);
177+
await client
178+
.one(analyzedExplainQuery, [tableName, 'objectId', caseInsensitiveData])
179+
.then(explained => {
180+
const preIndexPlan = explained;
181+
182+
preIndexPlan['QUERY PLAN'].forEach(element => {
183+
//Make sure search returned with only 1 result
184+
expect(element.Plan['Actual Rows']).toBe(1);
185+
expect(element.Plan['Node Type']).toBe('Seq Scan');
186+
});
187+
const indexName = 'test_case_insensitive_column';
188+
189+
adapter
190+
.ensureIndex(tableName, schema, ['objectId'], indexName, true)
191+
.then(() => {
192+
client
193+
.one(analyzedExplainQuery, [
194+
tableName,
195+
'objectId',
196+
caseInsensitiveData,
197+
])
198+
.then(explained => {
199+
const postIndexPlan = explained;
200+
201+
postIndexPlan['QUERY PLAN'].forEach(element => {
202+
//Make sure search returned with only 1 result
203+
expect(element.Plan['Actual Rows']).toBe(1);
204+
//Should not be a sequential scan
205+
expect(element.Plan['Node Type']).not.toContain('Seq Scan');
206+
207+
//Should be using the index created for this
208+
element.Plan.Plans.forEach(innerElement => {
209+
expect(innerElement['Index Name']).toBe(indexName);
210+
});
211+
});
212+
213+
//These are the same query so should be the same size
214+
for (let i = 0; i < preIndexPlan['QUERY PLAN'].length; i++) {
215+
//Sequential should take more time to execute than indexed
216+
expect(
217+
preIndexPlan['QUERY PLAN'][i]['Execution Time']
218+
).toBeGreaterThan(
219+
postIndexPlan['QUERY PLAN'][i]['Execution Time']
220+
);
221+
}
222+
223+
//Test explaining without analyzing
224+
const basicExplainQuery = adapter.createExplainableQuery(
225+
originalQuery
226+
);
227+
client
228+
.one(basicExplainQuery, [
229+
tableName,
230+
'objectId',
231+
caseInsensitiveData,
232+
])
233+
.then(explained => {
234+
explained['QUERY PLAN'].forEach(element => {
235+
//Check that basic query plans isn't a sequential scan
236+
expect(element.Plan['Node Type']).not.toContain(
237+
'Seq Scan'
238+
);
239+
240+
//Basic query plans shouldn't have an execution time
241+
expect(element['Execution Time']).toBeUndefined();
242+
});
243+
});
244+
});
245+
});
246+
})
247+
.catch(error => {
248+
// Query on non existing table, don't crash
249+
if (error.code !== '42P01') {
250+
throw error;
251+
}
252+
return [];
253+
});
254+
});
255+
256+
it('should use index for caseInsensitive query', async () => {
257+
const tableName = '_User';
258+
const user = new Parse.User();
259+
user.set('username', 'Bugs');
260+
user.set('password', 'Bunny');
261+
await user.signUp();
262+
const database = Config.get(Parse.applicationId).database;
263+
264+
//Postgres won't take advantage of the index until it has a lot of records because sequential is faster for small db's
265+
const client = adapter._client;
266+
await client.none(
267+
'INSERT INTO $1:name ($2:name, $3:name) SELECT MD5(random()::text), MD5(random()::text) FROM generate_series(1,5000)',
268+
[tableName, 'objectId', 'username']
269+
);
270+
const caseInsensitiveData = 'bugs';
271+
const fieldToSearch = 'username';
272+
//Check using find method for Parse
273+
const preIndexPlan = await database.find(
274+
tableName,
275+
{ username: caseInsensitiveData },
276+
{ caseInsensitive: true, explain: true }
277+
);
278+
279+
preIndexPlan.forEach(element => {
280+
element['QUERY PLAN'].forEach(innerElement => {
281+
//Check that basic query plans isn't a sequential scan, be careful as find uses "any" to query
282+
expect(innerElement.Plan['Node Type']).toBe('Seq Scan');
283+
//Basic query plans shouldn't have an execution time
284+
expect(innerElement['Execution Time']).toBeUndefined();
285+
});
286+
});
287+
288+
const indexName = 'test_case_insensitive_column';
289+
const schema = await new Parse.Schema('_User').get();
290+
await adapter.ensureIndex(
291+
tableName,
292+
schema,
293+
[fieldToSearch],
294+
indexName,
295+
true
296+
);
297+
298+
//Check using find method for Parse
299+
const postIndexPlan = await database.find(
300+
tableName,
301+
{ username: caseInsensitiveData },
302+
{ caseInsensitive: true, explain: true }
303+
);
304+
305+
postIndexPlan.forEach(element => {
306+
element['QUERY PLAN'].forEach(innerElement => {
307+
//Check that basic query plans isn't a sequential scan
308+
expect(innerElement.Plan['Node Type']).not.toContain('Seq Scan');
309+
310+
//Basic query plans shouldn't have an execution time
311+
expect(innerElement['Execution Time']).toBeUndefined();
312+
});
313+
});
314+
});
315+
316+
it('should use index for caseInsensitive query using default indexname', async () => {
317+
const tableName = '_User';
318+
const user = new Parse.User();
319+
user.set('username', 'Bugs');
320+
user.set('password', 'Bunny');
321+
await user.signUp();
322+
const database = Config.get(Parse.applicationId).database;
323+
const fieldToSearch = 'username';
324+
//Create index before data is inserted
325+
const schema = await new Parse.Schema('_User').get();
326+
await adapter.ensureIndex(tableName, schema, [fieldToSearch], null, true);
327+
328+
//Postgres won't take advantage of the index until it has a lot of records because sequential is faster for small db's
329+
const client = adapter._client;
330+
await client.none(
331+
'INSERT INTO $1:name ($2:name, $3:name) SELECT MD5(random()::text), MD5(random()::text) FROM generate_series(1,5000)',
332+
[tableName, 'objectId', 'username']
333+
);
334+
335+
const caseInsensitiveData = 'buGs';
336+
//Check using find method for Parse
337+
const indexPlan = await database.find(
338+
tableName,
339+
{ username: caseInsensitiveData },
340+
{ caseInsensitive: true, explain: true }
341+
);
342+
indexPlan.forEach(element => {
343+
element['QUERY PLAN'].forEach(innerElement => {
344+
expect(innerElement.Plan['Node Type']).not.toContain('Seq Scan');
345+
expect(innerElement.Plan['Index Name']).toContain('parse_default');
346+
});
347+
});
348+
});
148349
});
149350

150351
describe_only_db('postgres')('PostgresStorageAdapter shutdown', () => {

0 commit comments

Comments
 (0)