@@ -83,7 +83,9 @@ export class PgInstrumentation extends TdInstrumentationBase {
8383 // Submittable queries use EventEmitter pattern (row, end, error events)
8484 // and return the Query object itself, not a Promise
8585 if ( self . isSubmittable ( args [ 0 ] ) ) {
86- logger . debug ( `[PgInstrumentation] Submittable query detected, passing through uninstrumented` ) ;
86+ logger . debug (
87+ `[PgInstrumentation] Submittable query detected, passing through uninstrumented` ,
88+ ) ;
8789 return originalQuery . apply ( this , args ) ;
8890 }
8991
@@ -365,9 +367,25 @@ export class PgInstrumentation extends TdInstrumentationBase {
365367 return originalQuery . apply ( context , args ) ;
366368 } else {
367369 // Promise-based query
368- const promise = originalQuery . apply ( context , args ) ;
370+ const result = originalQuery . apply ( context , args ) ;
371+
372+ // ORMs like Prisma, TypeORM, etc. may call pg.query() internally in ways
373+ // that return undefined instead of a Promise (e.g. Prisma's driver adapter).
374+ // Without this guard, we'd crash with "Cannot read properties of undefined
375+ // (reading 'then')".
376+ if ( ! result || typeof result . then !== "function" ) {
377+ logger . debug (
378+ `[PgInstrumentation] originalQuery returned non-thenable, passing through (${ SpanUtils . getTraceInfo ( ) } )` ,
379+ ) ;
380+ try {
381+ SpanUtils . endSpan ( spanInfo . span , { code : SpanStatusCode . OK } ) ;
382+ } catch ( error ) {
383+ logger . error ( `[PgInstrumentation] error ending span:` , error ) ;
384+ }
385+ return result ;
386+ }
369387
370- return promise
388+ return result
371389 . then ( ( result : PgResult ) => {
372390 logger . debug (
373391 `[PgInstrumentation] PG query completed successfully (${ SpanUtils . getTraceInfo ( ) } )` ,
@@ -497,23 +515,25 @@ export class PgInstrumentation extends TdInstrumentationBase {
497515 *
498516 * Reference for data type IDs: https://jdbc.postgresql.org/documentation/publicapi/constant-values.html
499517 */
500- private convertPostgresTypes ( result : any , rowMode ?: ' array' ) : any {
518+ private convertPostgresTypes ( result : any , rowMode ?: " array" ) : any {
501519 // Handle multi-statement results (wrapped format from _addOutputAttributesToSpan)
502520 if ( result && result . isMultiStatement && Array . isArray ( result . results ) ) {
503- return result . results . map ( ( singleResult : any ) => this . convertPostgresTypes ( singleResult , rowMode ) ) ;
521+ return result . results . map ( ( singleResult : any ) =>
522+ this . convertPostgresTypes ( singleResult , rowMode ) ,
523+ ) ;
504524 }
505525
506526 // Handle multi-statement results (array of Results - legacy/direct format)
507527 if ( Array . isArray ( result ) ) {
508- return result . map ( singleResult => this . convertPostgresTypes ( singleResult , rowMode ) ) ;
528+ return result . map ( ( singleResult ) => this . convertPostgresTypes ( singleResult , rowMode ) ) ;
509529 }
510530
511531 if ( ! result || ! result . fields || ! result . rows ) {
512532 return result ;
513533 }
514534
515535 // If rowMode is 'array', handle arrays differently
516- if ( rowMode === ' array' ) {
536+ if ( rowMode === " array" ) {
517537 // For array mode, rows are arrays of values indexed by column position
518538 const convertedRows = result . rows . map ( ( row : any ) => {
519539 if ( ! Array . isArray ( row ) ) return row ; // Safety check
@@ -655,7 +675,7 @@ export class PgInstrumentation extends TdInstrumentationBase {
655675 // Wrap in object with 'results' key to ensure CLI can handle it properly
656676 outputValue = {
657677 isMultiStatement : true ,
658- results : result . map ( r => ( {
678+ results : result . map ( ( r ) => ( {
659679 command : r . command ,
660680 rowCount : r . rowCount ,
661681 oid : r . oid ,
0 commit comments