brianc / node-postgres

PostgreSQL client for node.js.

Home Page:https://node-postgres.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`\gexec` fails with `syntax error at or near "\"`

tukusejssirs opened this issue · comments

Related: StackOverflow


I want to run the following query:

SELECT 'CREATE DATABASE mydb'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'mydb')\gexec

This command works as expected in psql, however, it fails when run using pg-protocol via MikroORM. I am not sure if this is an issue of MikroORM, however, I don’t think so, as the log are coming from pg-protocol@1.6.1.

error: SELECT 'CREATE DATABASE mydb' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'mydb')\gexec - syntax error at or near "\"
    at Parser.parseErrorMessage ($repo_root/node_modules/.pnpm/pg-protocol@1.6.1/node_modules/pg-protocol/src/parser.ts:369:69)
    at Parser.handlePacket ($repo_root/node_modules/.pnpm/pg-protocol@1.6.1/node_modules/pg-protocol/src/parser.ts:188:21)
    at Parser.parse ($repo_root/node_modules/.pnpm/pg-protocol@1.6.1/node_modules/pg-protocol/src/parser.ts:103:30)
    at Socket.<anonymous> ($repo_root/node_modules/.pnpm/pg-protocol@1.6.1/node_modules/pg-protocol/src/index.ts:7:48)
    at Socket.emit (node:events:519:28)
    at addChunk (node:internal/streams/readable:559:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
    at Socket.Readable.push (node:internal/streams/readable:390:5)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
  length: 91,
  severity: 'ERROR',
  code: '42601',
  detail: undefined,
  hint: undefined,
  position: '130',
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'scan.l',
  line: '1241',
  routine: 'scanner_yyerror'
}

Is there a way to run commands with \gexec? Or do I need to move the logic (condition) to Node.js? 🤔

Thanks in advance! 🙏

Hey! You might need to escape the \ in the \gexec command - i'm not familiar w/ that so I can't say for sure...but the parse error is coming from the backend so it doesn't like that string coming in as a query. A bit of trail and error maybe? or someone smarter than me on the matter maybe can help? 🙇

Thanks, @brianc, for your comment! 🙏

Hey! You might need to escape the \ in the \gexec command

Well, I actually used \\gexec in Node.

  • When I used \gexec, \g would be removed (obviously) from the executed command, thus in the error too.
  • I presume \\gexec should work;
  • I have also tried \\\\gexec, but in the error, the command is output with double backslashes, which seems to me incorrect.

but the parse error is coming from the backend so it doesn't like that string coming in as a query. A bit of trail and error maybe? or someone smarter than me on the matter maybe can help? 🙇

Note that I haven’t looked into the code of your library.

I have just tested the query directly with pg, at the error is all the same:

// Query
await client.query(
  "SELECT 'CREATE DATABASE mydb' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'mydb')\\gexec"
)

// Error
error: syntax error at or near "\"
    at $repo_root/node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/client.js:526:17
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async REPL70:1:45 {
  length: 90,
  severity: 'ERROR',
  code: '42601',
  detail: undefined,
  hint: undefined,
  position: '96',
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'scan.l',
  line: '1241',
  routine: 'scanner_yyerror'
}

It looks like \gexec can be used in psql (CLI utility) only (cf SO).

If that’s so, sorry for opening this issue. Feel free to close it.

Thanks for your time! 🙏

no worries! I continually learn more about postgres & how to triage issues as these come in so it's useful for me too. ❤️