question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Query parameters handling causes syntax error

See original GitHub issue

When I do the following query, everything completes fine in psql:

-- psql
INSERT INTO public.contest (contest_id, period_id, start_ts, end_ts, contest_name, default_format, status ) 
VALUES ('VKVPA', '2019/01', timestamp '2019-01-20 08:00', 
timestamp '2019-01-20 11:00', 'description', 'EDI', 'NEW' ) RETURNING contest_key;

-- console output:
contest_key |
------------+
         17 |

(start_ts and end_ts have type TIMESTAMP WITHOUT TIME ZONE) When I do the same in a program using pg module, it ends with syntax error:

// contest-debug.ts
import { Pool } from 'pg' ;
let pool = new Pool( {user: 'contest_owner', database: 'contest'} );
pool.query(
    "INSERT INTO public.contest (contest_id, period_id, start_ts, end_ts, contest_name, default_format, status ) "
    + "VALUES ($1, $2, timestamp $3, timestamp $4, $5, $6, 'NEW' ) RETURNING contest_key", 
    ['VKVPA', '2019/02',  '2019-02-17 08:00', '2019-02-17 11:00', 'VKV Provozni aktiv 2019/02', 'EDI']
  )
  .then( result => { 
      console.log(`New contest has number ${result.rows[0].contest_key}`);
    })
  .catch( reason => { console.log( 'Contest creation failed:', reason )});

Console output:

Contest creation failed: { error: syntax error at or near "$3"
    at Connection.parseE (D:\dev\cav\log2any\node_modules\pg\lib\connection.js:601:11)
    at Connection.parseMessage (D:\dev\cav\log2any\node_modules\pg\lib\connection.js:398:19)
    at Socket.<anonymous> (D:\dev\cav\log2any\node_modules\pg\lib\connection.js:120:22)
    at Socket.emit (events.js:193:13)
    at addChunk (_stream_readable.js:296:12)
    at readableAddChunk (_stream_readable.js:277:11)
    at Socket.Readable.push (_stream_readable.js:232:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:150:17)
  name: 'error',
  length: 92,
  severity: 'ERROR',
  code: '42601',
  detail: undefined,
  hint: undefined,
  position: '135',
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'scan.l',
  line: '1134',
  routine: 'scanner_yyerror' }

The following code works. I did not pass the timestamp strings as parameters and instead included them directly in the text of the query. Apparently, this is a bug either in libpq or in the javascript module. For some strange reason a string parameter is not handled as string if it is preceded by timestamp keyword in the query,

pool.query(
    "INSERT INTO public.contest (contest_id, period_id, start_ts, end_ts, contest_name, default_format, status ) "
    + "VALUES ($1, $2, timestamp '2019-02-17 08:00', timestamp '2019-02-17 11:00', $3, $4, 'NEW' ) RETURNING contest_key", 
    ['VKVPA', '2019/02',  'VKV Provozni aktiv 2019/02', 'EDI']
  )
...

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
sehropecommented, Apr 9, 2019

It’s not possible to specify parameter values for the literal portion of SQL standard TIMESTAMP '...' literals. It’s not specific to this driver or libpq, it’s a server limitation that you can’t specify a parameter when using that particular syntax.

As a workaround you can use the PostgreSQL specific casting syntax: $3::timestamp

Also, you may want to look into using timestamptz rather than timestamp (with no tz).

0reactions
radio-miskovicecommented, Apr 9, 2019

BTW, where could I find the syntax note? I was reading through sections 8.5 and 9.9 and contrary to what is needed in the code, they only mention the syntax that maybe works fine in psql only, i,e, TIMESTAMP followed by a ISO-8601 string. It’s kind of confusing. I will post a correction request in the doc section. Actually in standard SQL it would be CAST( $3 AS TIMESTAMP ) which works perfectly well but who knows why they do not even mention it in the manual (section 8.5 and 9.9).

Read more comments on GitHub >

github_iconTop Results From Across the Web

I get (Syntax error in string in query expression ''WHERE [ID]='.)
I have trying many solution but i am still no success solve the problem.when user add data success the data will success save...
Read more >
Common SQL syntax errors and how to resolve them
In this article, we are going to describe some of the most common SQL syntax errors, and explains how you can resolve these...
Read more >
Ad-hoc query string causes syntax error - Stack Overflow
You haven't closed the string identifier {0} with apostrophe in format string: query = string.Format("SELECT password FROM users WHERE name ...
Read more >
SyntaxError: missing formal parameter - JavaScript | MDN
The JavaScript exception "missing formal parameter" occurs when your function declaration is missing valid parameters.
Read more >
When invalid querystring paramater detected in a web page url
I'd always throw the appropriate error. For a web site a malformed query string may return HTTP 400 status code (Bad Request, read...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found