jasci
06/14/2021, 7:08 AMError querying the database: db error: FATAL: sorry, too many clients alreadyRyan
06/14/2021, 7:16 AMjasci
06/14/2021, 7:22 AMRyan
06/14/2021, 7:29 AMprisma.$disconnect() after each test suite?Ryan
06/14/2021, 7:30 AMjasci
06/14/2021, 7:49 AMnpx prisma migrate reset --force before and after each test suite.
I just thought that I don’t have that many tests and nevertheless the API basically stopped working. How to properly manage that situation in the production ?
I can imagine that many requests to the API from real users, and definitely don’t want the API to crash.jasci
06/14/2021, 7:59 AMprisma.$disconnect() ? I don’t use prisma in tests (only resetting db). I wanted the tests to not be tied to the tech I’m using in the backend. Just simple queries with apollo client and check the result.janpio
Ryan
06/14/2021, 11:06 AMBasically I test the API, just make queries with apollo client as if it’s just some user.This must be running your server under the hood right? This opens a connection to the database and will not be closed automatically after the test is complete. So you need to call
prisma.$disconnect() so that the connection is freed and there’s no connection overload.
Also, I runI wouldn’t personally recommend this and advise to run each test suite against a new schema so that test suites can also run in parallel.before and after each test suite.npx prisma migrate reset --force
jasci
06/14/2021, 11:36 AMSo you need to callBut how should I handle it in the production, when users are making queries to the API ? It can have the same error, so I need to callso that the connection is freed and there’s no connection overload.prisma.$disconnect()
prisma.$disconnect() too ? How to properly manage it ?
What command exactly is giving you this error message then?After certain amount of tests this error occurs in all the next tests with different models
ApolloError:
Invalid `prisma.constant.create()` invocation:
Error querying the database: db error: FATAL: sorry, too many clients already
`Invalid `prisma.constant.create()`` , `Invalid `prisma.user.create()`` ,
`Invalid `prisma.company.create()`` , etc.Ryan
06/14/2021, 11:37 AMBut how should I handle it in the production, when users are making queries to the API ? It can have the same error, so I need to callThis will not happen in production as your server will be running always unlike tests that end after a specific time.too ? How to properly manage it ?prisma.$disconnect()
jasci
06/14/2021, 11:40 AMRyan
06/14/2021, 11:42 AMjasci
06/14/2021, 11:53 AMconst CHANGE_ORDER_STATUS_MUTATION = gql`
mutation ChangeOrderStatusMutation($changeOrderStatusWhere: OrderWhereUniqueInput!, $changeOrderStatusData: OrderChangeStatusInput!) {
changeOrderStatus(where: $changeOrderStatusWhere, data: $changeOrderStatusData) {
slug
title
description
comment
status
parentSalesDomain {
slug
}
}
}
`;
let result;
try {
result = await client.mutate({
mutation: CHANGE_ORDER_STATUS_MUTATION,
variables: {
changeOrderStatusWhere: { slug: orderSlug },
changeOrderStatusData: { status: orderStatusUpdated }
}
})
} catch (error) {
errorLog(error);
}
So basically tests are fully separated from the API. Tests are run against an actual server using dev database(just local postgres container).jasci
06/15/2021, 9:23 AMreset command too.
Command failed: npx prisma migrate reset --force
Error: undefined: Database error
Error querying the database: db error: FATAL: sorry, too many clients alreadyRyan
06/15/2021, 9:41 AMjasci
06/15/2021, 10:32 AMjasci
06/15/2021, 10:37 AMprisma:info Starting a postgresql pool with 13 connections.
prisma:info Encountered error during initialization:
prisma:error in 0: backtrace::backtrace::trace
1: backtrace::capture::Backtrace::new
2: user_facing_errors::Error::new_non_panic_with_current_backtrace
3: query_engine::error::<impl core::convert::From<query_engine::error::PrismaError> for user_facing_errors::Error>::from
4: query_engine::main::main::{{closure}}::{{closure}}
5: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
6: std::thread::local::LocalKey<T>::with
7: <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll
8: async_io::driver::block_on
9: async_global_executor::reactor::block_on
10: std::thread::local::LocalKey<T>::with
11: async_std::task::builder::Builder::blocking
12: query_engine::main
13: std::sys_common::backtrace::__rust_begin_short_backtrace
14: std::rt::lang_start::{{closure}}
15: std::rt::lang_start_internal
16: _main
Error querying the database: db error: FATAL: sorry, too many clients alreadyjasci
06/15/2021, 10:39 AMwarn(prisma-client) Already 10 Prisma Clients are actively running.Ryan
06/15/2021, 10:41 AMjasci
06/15/2021, 11:07 AMjasci
06/15/2021, 11:07 AMcontext argument to the ApolloServer is a function that is called with every request, I thought it’s called once at the start and then resulting object is passed by. And I was creating an instance of the prisma client in that context function.
But now when I’m creating prismaClient once, I get this error when I run several test suites.
console.error src/tests/utils/ErrorLog.ts:7
Exception: {
errors: [
{
message: '\n' +
'\x1B[31mInvalid \x1B[1m`prisma.company.create()`\x1B[22m invocation:\x1B[39m\n' +
'\n' +
'\n' +
' Error occurred during query execution:\n' +
'ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(Error { kind: Db, cause: Some(DbError { severity: "ERROR", parsed_severity: Some(Error), code: SqlState("XX000"), message: "cache lookup failed for type 685238", detail: None, hint: None, position: None, where_: None, schema: None, table: None, column: None, datatype: None, constraint: None, file: Some("lsyscache.c"), line: Some(2672), routine: Some("getTypeBinaryInputInfo") }) }) })',
locations: [],
path: [Array]
}
],
stacktrace: [
'Error: ',
'\x1B[31mInvalid \x1B[1m`prisma.company.create()`\x1B[22m invocation:\x1B[39m',
'',
'',
' Error occurred during query execution:',
'ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(Error { kind: Db, cause: Some(DbError { severity: "ERROR", parsed_severity: Some(Error), code: SqlState("XX000"), message: "cache lookup failed for type 685238", detail: None, hint: None, position: None, where_: None, schema: None, table: None, column: None, datatype: None, constraint: None, file: Some("lsyscache.c"), line: Some(2672), routine: Some("getTypeBinaryInputInfo") }) }) })',
' at new CombinedError (/Users/jackiejo/main/smidyo/smidyo-monorepo/quoting-portal/api/node_modules/apollo-server/node_modules/graphql-tools/src/stitching/errors.ts:90:5)',
' at Object.checkResultAndHandleErrors (/Users/jackiejo/main/smidyo/smidyo-monorepo/quoting-portal/api/node_modules/apollo-server/node_modules/graphql-tools/src/stitching/errors.ts:111:11)',
' at CheckResultAndHandleErrors.transformResult (/Users/jackiejo/main/smidyo/smidyo-monorepo/quoting-portal/api/node_modules/apollo-server/node_modules/graphql-tools/src/transforms/CheckResultAndHandleErrors.ts:15:12)',
' at /Users/jackiejo/main/smidyo/smidyo-monorepo/quoting-portal/api/node_modules/apollo-server/node_modules/graphql-tools/src/transforms/transforms.ts:37:45',
' at Array.reduce (<anonymous>)',
' at applyResultTransforms (/Users/jackiejo/main/smidyo/smidyo-monorepo/quoting-portal/api/node_modules/apollo-server/node_modules/graphql-tools/src/transforms/transforms.ts:35:21)',
' at /Users/jackiejo/main/smidyo/smidyo-monorepo/quoting-portal/api/node_modules/apollo-server/node_modules/graphql-tools/src/stitching/delegateToSchema.ts:104:12',
' at step (/Users/jackiejo/main/smidyo/smidyo-monorepo/quoting-portal/api/node_modules/apollo-server/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:32:23)',
' at Object.next (/Users/jackiejo/main/smidyo/smidyo-monorepo/quoting-portal/api/node_modules/apollo-server/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:13:53)',
' at fulfilled (/Users/jackiejo/main/smidyo/smidyo-monorepo/quoting-portal/api/node_modules/apollo-server/node_modules/graphql-tools/dist/stitching/delegateToSchema.js:4:58)'
]
}
console.error src/tests/utils/ErrorLog.ts:8
Locations: [ { line: 2, column: 3 } ]
console.error src/tests/utils/ErrorLog.ts:9
Path: [ 'createCompany' ]
console.error src/tests/utils/ErrorLog.ts:15
ERROR: ApolloError:
Invalid `prisma.company.create()` invocation:janpio
Ryan
06/15/2021, 11:51 AMjasci
06/15/2021, 1:19 PMRyan
06/15/2021, 1:27 PMRyan
06/15/2021, 1:28 PMjasci
06/15/2021, 2:15 PMjasci
06/15/2021, 2:15 PMconsole.error src/tests/utils/ErrorLog.ts:7
Exception: {
errors: [
{
message: '\n' +
'Invalid `prisma.salesDomain.update()` invocation:\n' +
'\n' +
'\n' +
' Error in PostgreSQL connection: Error { kind: Db, cause: Some(DbError { severity: "FATAL", parsed_severity: Some(Fatal), code: SqlState("57P01"), message: "terminating connection due to unexpected postmaster exit", detail: None, hint: None, position: None, where_: None, schema: None, table: None, column: None, datatype: None, constraint: None, file: Some("be-secure.c"), line: Some(189), routine: Some("secure_read") }) }\n' +
'\n' +
'This is a non-recoverable error which probably happens when the Prisma Query Engine has a panic.\n' +
'\n' +
'<https://github.com/prisma/prisma/issues/new?body=Hi+Prisma+Team%21+My+Prisma+Client+just+crashed.+This+is+the+report%3A%0A%23%23+Versions%0A%0A%7C+Name++++++++++++%7C+Version++++++++++++%7C%0A%7C-----------------%7C--------------------%7C%0A%7C+Node++++++++++++%7C+v14.15.1+++++++++++%7C+%0A%7C+OS++++++++++++++%7C+darwin+++++++++++++%7C%0A%7C+Prisma+Client+++%7C+2.24.1+++++++++++++%7C%0A%7C+Query+Engine++++%7C+query-engine+18095475d5ee64536e2f93995e48ad800737a9e4%7C%0A%7C+Database++++++++%7C+undefined%7C%0A%0A%0A%0A%23%23+Query%0A%60%60%60%0Amutation+%7B%0A++updateOneSalesDomain%28%0A++++where%3A+%7B%0A++++++slug%3A+%22X%22%0A++++%7D%0A++++data%3A+%7B%0A++++++shippingMethodOrder%3A+%7B%0A++++++++set%3A+%5B%22X%22%5D%0A++++++%7D%0A++++%7D%0A++%29+%7B%0A++++id%0A++++slug%0A++++customDomain%0A++++onboarded%0A++++title%0A++++website%0A++++thankYouPage%0A++++privacyPolicy%0A++++translationOverrides%0A++++lightLogoUrl%0A++++darkLogoUrl%0A++++backdropUrl%0A++++brandColor%0A++++removeSmidyoBranding%0A++++googleAnalyticsID%0A++++googleTagManagerID%0A++++hotjarID%0A++++allowPublicAccess%0A++++allowPublicQuoting%0A++++allowPublicAccountCreation%0A++++offeringFormAutoScroll%0A++++subRowsDisplayMode%0A++++homePageText%0A++++hideMenuIfSingleOffering%0A++++taxPercentage%0A++++taxShowMode%0A++++taxCalculationMode%0A++++quoteRowInfoDisplayMode%0A++++accountDisplayAdditionalOrderInfoInput%0A++++accountAllowManualQuote%0A++++accountOnlyAllowOneActiveQuote%0A++++dateFormat%0A++++offlineAccountShowCompanyField%0A++++offlineAccountPhoneField%0A++++accountAddress%0A++++accountAddressShowRegionField%0A++++accountAddressShowContactPerson%0A++++accountAddressShowCountryField%0A++++requireAddressForOrdering%0A++++offeringQuotePopupBehavior%0A++++currency%0A++++stripePublishableKey%0A++++shippingEnabled%0A++++nextOrderNumber%0A++++baseCostCalculationMode%0A++++useCustomQuoteTemplate%0A++++useCustomReceiptTemplate%0A++++stripeConnectedAccountID%0A++++paypalClientID%0A++++paypalSecretSecretsManagerARN%0A++++klarnaUsername%0A++++klarnaPasswordSecretsManagerARN%0A++++nextQuoteNumber%0A++++cloudFrontDistributionARN%0A++++acmARN%0A++++parentCompanyId%0A++++translationId%0A++++shippingMethodOrder%0A++++accountDefaultPaymentMethods%0A++++offlineAccountPaymentMethods%0A++%7D%0A%7D%0A%60%60%60%0A%0A%23%23+Logs%0A%60%60%60%0A%5B0m%0A++prisma%3Aengine+%7B%0A++prisma%3Aengine+++error%3A+PrismaClientUnknownRequestError2+%5BPrismaClientUnknownRequestError%5D%3A+Error+occurred+during+query+execution%3A%0A++prisma%3Aengine+++ConnectorError%28ConnectorError+%7B+user_facing_error%3A+None%2C+kind%3A+QueryError%28Error+%7B+kind%3A+Db%2C+cause%3A+Some%28DbError+%7B+severity%3A+%22ERROR%22%2C+parsed_severity%3A+Some%28Error%29%2C+code%3A+SqlState%28%220A000%22%29%2C+message%3A+%22cached+plan+must+not+change+result+type%22%2C+detail%3A+None%2C+hint%3A+None%2C+position%3A+None%2C+where_%3A+None%2C+schema%3A+None%2C+table%3A+None%2C+column%3A+None%2C+datatype%3A+None%2C+constraint%3A+None%2C+file%3A+Some%28%22plancache.c%22%29%2C+line%3A+Some%28722%29%2C+routine%3A+Some%28%22RevalidateCachedQuery%22%29+%7D%29+%7D%29+%7D%29%0A++prisma%3Aengine+++++++at+NodeEngine.graphQLToJSError+%28%2FUsers%2Fjackiejo%2Fmain%2Fsmidyo%2Fsmidyo-monorepo%2Fquoting-portal%2Fapi%2Fnode_modules%2F%40prisma%2Fclient%2Fruntime%2Findex.js%3A28377%3A14%29%0A++prisma%3Aengine+++++++at+NodeEngine.request+%28%2FUsers%2Fjackiejo%2Fmain%2Fsmidyo%2Fsmidyo-monorepo%2Fquoting-portal%2Fapi%2Fnode_modules%2F%40prisma%2Fclient%2Fruntime%2Findex.js%3A28273%3A24%29%0A++prisma%3Aengine+++++++at+runMicrotasks+%28%3Canonymous%3E%29%0A++prisma%3Aengine+++++++at+processTicksAndRejections+%28internal%2Fprocess%2Ftask_queues.js%3A93%3A5%29%0A++prisma%3Aengine+++++++at+cb+%28%2FUsers%2Fjackiejo%2Fmain%2Fsmidyo%2Fsmidyo-monorepo%2Fquoting-portal%2Fapi%2Fnode_modules%2F%40prisma%2Fclient%2Fruntime%2Findex.js%3A35193%3A26%29%0A++prisma%3Aengine+++++++at+Object.createShippingMethod+%28%2FUsers%2Fjackiejo%2Fmain%2Fsmidyo%2Fsmidyo-monorepo%2Fquoting-portal%2Fapi%2Fsrc%2Fresolvers%2Fsales-domain%2Fmutations%2Fshipping-method%2Fmutations.ts%3A25%3A3%29%0A++prisma%3Aengine+++++++at+createSalesDomain+%28%2FUsers%2Fjackiejo%2Fmain%2Fsmidyo%2Fsmidyo-monorepo%2Fquoting-portal%2Fapi%2Fsrc%2Fresolvers%2Fsales-domain%2Fmutations%2Fcrud%2Fmutations.ts%3A41%3A3%29+%7B%0A++prisma%3Aengine+++++clientVersion%3A+%272.24.1%27%0A++prisma%3Aengine+++%7D%0A++prisma%3Aengine+%7D++%0A++prisma%3Aengine+stdout++Fetched+a+connection+from+the+pool++%0A++prisma%3Aengine+stdout++Unknown+error++%0A++prisma%3Aengine+stdout++Unknown+error++%0A++prisma%3Aengine+stdout++Unknown+error++%0A++prisma%3Aengine+stdout++Unknown+error++%0A++prisma%3Aengine+stdout++Unknown+error++%0A++prisma%3Aengine+%7B%0A++prisma%3Aengine+++error%3A+PrismaClientUnknownRequestError2+%5BPrismaClientUnknownRequestError%5D%3A+Error+occurred+during+query+execution%3A%0A++prisma%3Aengine+++ConnectorError%28ConnectorError+%7B+user_facing_error%3A+None%2C+kind%3A+QueryError%28Error+%7B+kind%3A+Db%2C+cause%3A+Some%28DbError+%7B+severity%3A+%22ERROR%22%2C+parsed_severity%3A+Some%28Error%29%2C+code%3A+SqlState%28%220A000%22%29%2C+message%3A+%22cached+plan+must+not+change+result+type%22%2C+detail%3A+None%2C+hint%3A+None%2C+position%3A+None%2C+where_%3A+None%2C+schema%3A+None%2C+table%3A+None%2C+column%3A+None%2C+datatype%3A+None%2C+constraint%3A+None%2C+file%3A+Some%28%22plancache.c%22%29%2C+line%3A+Some%28722%29%2C+routine%3A+Some%28%22RevalidateCachedQuery%22%29+%7D%29+%7D%29+%7D%29%0A++prisma%3Aengine+++++++at+NodeEngine.graphQLToJSError+%28%2FUsers%2Fjackiejo%2Fmain%2Fsmidyo%2Fsmidyo-monorepo%2Fquoting-portal%2Fapi%2Fnode_modules%2F%40prisma%2Fclient%2Fruntime%2Findex.js%3A28377%3A14%29%0A++prisma%3Aengine+++++++at+NodeEngine.request+%28%2FUsers%2Fjackiejo%2Fmain%2Fsmidyo%2Fsmidyo-monorepo%2Fquoting-portal%2Fapi%2Fnode_modules%2F%40prisma%2Fclient%2Fruntime%2Findex.js%3A28273%3A24%29%0A++prisma%3Aengine+++++++at+runMicrotasks+%28%3Canonymous%3E%29%0A++prisma%3Aengine+++++++at+processTicksAndRejections+%28internal%2Fprocess%2Ftask_queues.js%3A93%3A5%29%0A++prisma%3Aengine+++++++at+cb+%28%2FUsers%2Fjackiejo%2Fmain%2Fsmidyo%2Fsmidyo-monorepo%2Fquoting-portal%2Fapi%2Fnode_modules%2F%40prisma%2Fclient%2Fruntime%2Findex.js%3A35193%3A26%29%0A++prisma%3Aengine+++++++at+Object.createShippingMethod+%28%2FUsers%2Fjackiejo%2Fmain%2Fsmidyo%2Fsmidyo-monorepo%2Fquoting-portal%2Fapi%2Fsrc%2Fresolvers%2Fsales-domain%2Fmutations%2Fshipping-method%2Fmutations.ts%3A25%3A3%29%0A++prisma%3Aengine+++++++at+createSalesDomain+%28%2FUsers%2Fjackiejo%2Fmain%2Fsmidyo%2Fsmidyo-monorepo%2Fquoting-portal%2Fapi%2Fsrc%2Fresolvers%2Fsales-domain%2Fmutations%2Fcrud%2Fmutations.ts%3A41%3A3%29+%7B%0A++prisma%3Aengine+++++clientVersion%3A+%272.24.1%27%0A++prisma%3Aengine+++%7D%0A++prisma%3Aengine+%7D++%0A%60%60%60%0A%0A%23%23+Client+Snippet%0A%60%60%60ts%0A%2F%2F+PLEASE+FILL+YOUR+CODE+SNIPPET+HERE%0A%60%60%60%0A%0A%23%23+Schema%0A%60%60%60prisma%0A%2F%2F+PLEASE+ADD+YOUR+SCHEMA+HERE+IF+POSSIBLE%0A%60%60%60%0A&title=Error+in+PostgreSQL+connection%3A+Error+%7B+kind%3A+Db%2C+cause%3A+Some%28DbError+%7B+severity%3A+%22FATAL%22%2C+parsed_severity%3A+Some%28Fatal%29%2C+code%3A+SqlState%28%2257P01%22%29%2C+message%3A+%22terminating+connection+due+to+unexpected+postmaster+exit%22%2C+detail%3A+None%2C+hint%3A+None%2C+position%3A+None%2C+where_%3A+None%2C+schema%3A+None%2C+table%3A+None%2C+column%3A+None%2C+datatype%3A+None%2C+constraint%3A+None%2C+file%3A+Some%28%22be-secure.c%22%29%2C+line%3A+Some%28189%29%2C+routine%3A+Some%28%22secure_read%22%29+%7D%29+%7D&template=bug_report.md>\n' +
'\n' +
'If you want the Prisma team to look into it, please open the link above 🙏\n' +
'To increase the chance of success, please post your schema and a snippet of\n' +
'how you used Prisma Client in the issue. \n',
locations: [],
path: [Array]
}
],
stacktrace: [
'Error: ',
'Invalid `prisma.salesDomain.update()` invocation:',
'',
'',
' Error in PostgreSQL connection: Error { kind: Db, cause: Some(DbError { severity: "FATAL", parsed_severity: Some(Fatal), code: SqlState("57P01"), message: "terminating connection due to unexpected postmaster exit", detail: None, hint: None, position: None, where_: None, schema: None, table: None, column: None, datatype: None, constraint: None, file: Some("be-secure.c"), line: Some(189), routine: Some("secure_read") }) }',
'',
'This is a non-recoverable error which probably happens when the Prisma Query Engine has a panic.',
'',
....
]
}Ryan
06/15/2021, 2:17 PMjanpio
janpio
jasci
06/15/2021, 3:28 PMnpx prisma migrate reset command at the start and at the end of each test suite. It resets the db and then apply migrations, but postgres cache some queries, and at some point it can be that cached database commands are wrong due to running migrations or something like that (cached queries cannot be applied to the current database stage). Or the reason cannot be in the reset command ?
I had these logs from docker postgres:
ERROR: cached plan must not change result type
and
ERROR: cache lookup failed for type 899836janpio
janpio
jasci
06/17/2021, 6:13 PMjanpio
jasci
06/18/2021, 7:35 AMjasci
06/22/2021, 9:32 AM&statement_cache_size=0 as a workaround. But for me it doesn’t solve the problem and I haven’t found this argument in the docs.
https://github.com/prisma/prisma/issues/7678#issuecomment-864967237jasci
06/22/2021, 9:34 AMjanpio
jasci
06/22/2021, 9:47 AM&statment_cache_size=0 should be &statement_cache_size=0 .
Wanted to ask where this command is comming from ?
In the prisma docs it’s not mentioned, also PostgreSQLl format doesn’t seems to describe it.janpio
janpio
janpio
jasci
06/22/2021, 11:05 AM