Skip to content

Commit 904e547

Browse files
committed
Merge branch 'test-cicd/upgrade-postgresql-to-13' of github.com:supertokens/supertokens-postgresql-plugin into test-cicd/upgrade-postgresql-to-13
2 parents 1c78d8f + f1359a4 commit 904e547

File tree

5 files changed

+59
-18
lines changed

5 files changed

+59
-18
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
1010
## [7.3.0]
1111

1212
- Adds tables and queries for Bulk Import
13+
- Optimize getUserIdMappingWithEitherSuperTokensUserIdOrExternalUserId query
1314

1415
### Migration
1516

@@ -34,6 +35,7 @@ CREATE INDEX IF NOT EXISTS bulk_import_users_pagination_index1 ON bulk_import_us
3435
3536
CREATE INDEX IF NOT EXISTS bulk_import_users_pagination_index2 ON bulk_import_users (app_id, created_at DESC, id DESC);
3637
```
38+
3739
## [7.2.0] - 2024-10-03
3840
3941
- Compatible with plugin interface version 6.3

jar/postgresql-plugin-7.3.0.jar

280 Bytes
Binary file not shown.

src/main/java/io/supertokens/storage/postgresql/queries/SessionQueries.java

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,6 @@ public static void createNewSession(Start start, TenantIdentifier tenantIdentifi
126126
public static SessionInfo getSessionInfo_Transaction(Start start, Connection con, TenantIdentifier tenantIdentifier,
127127
String sessionHandle)
128128
throws SQLException, StorageQueryException {
129-
// we do this as two separate queries and not one query with left join cause psql does not
130-
// support left join with for update if the right table returns null.
131-
132129
String QUERY =
133130
"SELECT session_handle, user_id, refresh_token_hash_2, session_data, " +
134131
"expires_at, created_at_time, jwt_user_payload, use_static_key FROM " +
@@ -149,21 +146,55 @@ public static SessionInfo getSessionInfo_Transaction(Start start, Connection con
149146
return null;
150147
}
151148

152-
QUERY = "SELECT primary_or_recipe_user_id FROM " + getConfig(start).getUsersTable()
153-
+ " WHERE app_id = ? AND user_id = ?";
154-
155-
return execute(con, QUERY, pst -> {
149+
QUERY = "SELECT external_user_id " +
150+
"FROM " + getConfig(start).getUserIdMappingTable() + " um2 " +
151+
"WHERE um2.app_id = ? AND um2.supertokens_user_id IN (" +
152+
"SELECT primary_or_recipe_user_id " +
153+
"FROM " + getConfig(start).getUsersTable() + " " +
154+
"WHERE app_id = ? AND user_id IN (" +
155+
"SELECT um1.supertokens_user_id as user_id " +
156+
"FROM " + getConfig(start).getUserIdMappingTable() + " um1 " +
157+
"WHERE um1.app_id = ? AND um1.external_user_id = ? " +
158+
"UNION ALL " +
159+
"SELECT ? " +
160+
"LIMIT 1" +
161+
")" +
162+
") " +
163+
"UNION ALL " +
164+
"SELECT primary_or_recipe_user_id " +
165+
"FROM " + getConfig(start).getUsersTable() + " " +
166+
"WHERE app_id = ? AND user_id IN (" +
167+
"SELECT um1.supertokens_user_id as user_id " +
168+
"FROM " + getConfig(start).getUserIdMappingTable() + " um1 " +
169+
"WHERE um1.app_id = ? AND um1.external_user_id = ? " +
170+
"UNION ALL " +
171+
"SELECT ? " +
172+
"LIMIT 1" +
173+
") " +
174+
"LIMIT 1";
175+
176+
String finalUserId = execute(con, QUERY, pst -> {
156177
pst.setString(1, tenantIdentifier.getAppId());
157-
pst.setString(2, sessionInfo.recipeUserId);
178+
pst.setString(2, tenantIdentifier.getAppId());
179+
pst.setString(3, tenantIdentifier.getAppId());
180+
pst.setString(4, sessionInfo.recipeUserId);
181+
pst.setString(5, sessionInfo.recipeUserId);
182+
pst.setString(6, tenantIdentifier.getAppId());
183+
pst.setString(7, tenantIdentifier.getAppId());
184+
pst.setString(8, sessionInfo.recipeUserId);
185+
pst.setString(9, sessionInfo.recipeUserId);
158186
}, result -> {
159187
if (result.next()) {
160-
String primaryUserId = result.getString("primary_or_recipe_user_id");
161-
if (primaryUserId != null) {
162-
sessionInfo.userId = primaryUserId;
163-
}
188+
return result.getString(1);
164189
}
165-
return sessionInfo;
190+
return sessionInfo.recipeUserId;
166191
});
192+
193+
if (finalUserId != null) {
194+
sessionInfo.userId = finalUserId;
195+
}
196+
197+
return sessionInfo;
167198
}
168199

169200
public static void updateSessionInfo_Transaction(Start start, Connection con, TenantIdentifier tenantIdentifier,

src/main/java/io/supertokens/storage/postgresql/queries/UserIdMappingQueries.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,16 @@ public static UserIdMapping[] getUserIdMappingWithEitherSuperTokensUserIdOrExter
139139
String userId)
140140
throws SQLException, StorageQueryException {
141141
String QUERY = "SELECT * FROM " + Config.getConfig(start).getUserIdMappingTable()
142-
+ " WHERE app_id = ? AND (supertokens_user_id = ? OR external_user_id = ?)";
142+
+ " WHERE app_id = ? AND supertokens_user_id = ?"
143+
+ " UNION ALL "
144+
+ "SELECT * FROM " + Config.getConfig(start).getUserIdMappingTable()
145+
+ " WHERE app_id = ? AND external_user_id = ?";
143146

144147
return execute(start, QUERY, pst -> {
145148
pst.setString(1, appIdentifier.getAppId());
146149
pst.setString(2, userId);
147-
pst.setString(3, userId);
150+
pst.setString(3, appIdentifier.getAppId());
151+
pst.setString(4, userId);
148152
}, result -> {
149153
ArrayList<UserIdMapping> userIdMappingArray = new ArrayList<>();
150154
while (result.next()) {
@@ -375,12 +379,16 @@ public static UserIdMapping[] getUserIdMappingWithEitherSuperTokensUserIdOrExter
375379
String userId)
376380
throws SQLException, StorageQueryException {
377381
String QUERY = "SELECT * FROM " + Config.getConfig(start).getUserIdMappingTable()
378-
+ " WHERE app_id = ? AND (supertokens_user_id = ? OR external_user_id = ?)";
382+
+ " WHERE app_id = ? AND supertokens_user_id = ?"
383+
+ " UNION ALL "
384+
+ "SELECT * FROM " + Config.getConfig(start).getUserIdMappingTable()
385+
+ " WHERE app_id = ? AND external_user_id = ?";
379386

380387
return execute(sqlCon, QUERY, pst -> {
381388
pst.setString(1, appIdentifier.getAppId());
382389
pst.setString(2, userId);
383-
pst.setString(3, userId);
390+
pst.setString(3, appIdentifier.getAppId());
391+
pst.setString(4, userId);
384392
}, result -> {
385393
ArrayList<UserIdMapping> userIdMappingArray = new ArrayList<>();
386394
while (result.next()) {

src/test/java/io/supertokens/storage/postgresql/test/OneMillionUsersTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ public void testWithOneMillionUsers() throws Exception {
990990

991991
Main main = startCronProcess(String.valueOf(4)); // ci uses instances with 4 cores..
992992

993-
int NUMBER_OF_USERS_TO_UPLOAD = 300000;
993+
int NUMBER_OF_USERS_TO_UPLOAD = 300000; // 300k
994994

995995
if (StorageLayer.getBaseStorage(main).getType() != STORAGE_TYPE.SQL || StorageLayer.isInMemDb(main)) {
996996
return;

0 commit comments

Comments
 (0)