sql

All things SQL and JDBC...
nick 2020-07-24T05:38:11.322300Z

If anyone might have an idea why 1st request returns some data and 2nd one fails, please share your thoughts

(next.jdbc/execute-one! t-conn
                        ["SELECT * FROM foo WHERE id = ?" 123])

(next.jdbc/execute-one! t-conn
                        ["COPY(SELECT * FROM foo WHERE id = ?) TO '/tmp/foo.csv' WITH CSV DELIMITER ',' HEADER" 123])
Execution error (PSQLException) at org.postgresql.core.v3.QueryExecutorImpl/receiveErrorResponse (QueryExecutorImpl.java:2532).
ERROR: there is no parameter $1
  Position: 36
A bit of context: I'm using honeysql to construct a huge structure which is used for displaying records(total+pagination) and also for exporting it as CSV file, so I'm passing this SELECT query inside COPY psql function.

nick 2020-07-24T05:39:23.323200Z

These 2 queries look very similar.. same number of arguments. No idea why in 2nd case it fails to find a parameter

seancorfield 2020-07-24T05:42:58.324500Z

@nfedyashev Just to clarify, since you talk about HoneySQL and a "huge structure", are the two queries above the repro case without all of that complexity? So, the actual queries that work and fail respectively?

seancorfield 2020-07-24T05:43:46.325200Z

(i.e., the failing case is something I could actually add to the next.jdbc test suite for PG and expect it to "work" if I can figure out what the issue is 🙂 )

nick 2020-07-24T05:50:14.326700Z

@seancorfield this should be the shortest code to reproduce the issue(2nd function fails):

(next.jdbc/execute-one! t-conn
                          ["SELECT 1 WHERE 2 = ?" 2])

  (next.jdbc/execute-one! t-conn
                          ["COPY(SELECT 1 WHERE 2 = ?) TO '/tmp/foo.csv' WITH CSV DELIMITER ',' HEADER" 2])
Execution error (PSQLException) at org.postgresql.core.v3.QueryExecutorImpl/receiveErrorResponse (QueryExecutorImpl.java:2532).
ERROR: there is no parameter $1
  Position: 25

nick 2020-07-24T05:50:38.327200Z

If I execute this copy command in psql, it works fine:

COPY(SELECT 1 WHERE 2 = 2) TO '/tmp/foo.csv' WITH CSV DELIMITER ',' HEADER;
COPY 1

nick 2020-07-24T05:53:59.327700Z

PostgreSQL 11.7 next.jdbc "1.1.547"

nick 2020-07-24T06:01:17.329900Z

@seancorfield thanks for your time Sean! Let me know if you need any additional info. Not sure if I fully understood your clarification question. That looks like next.jdbc level issue, not HoneySQL.

seancorfield 2020-07-24T06:04:42.330700Z

Yup, you clarified. Thanks. I'll create an issue and investigate tomorrow or over the weekend. It's an odd one. But, hey, PostgreSQL is its own kind of special...

seancorfield 2020-07-24T06:05:50.330900Z

https://github.com/seancorfield/next-jdbc/issues/136

seancorfield 2020-07-24T06:17:59.331700Z

@nfedyashev I can repro that in the test suite. Thank you! Now I just have to keep poking at it until I figure out why it breaks 🙂

🙏 1
seancorfield 2020-07-24T06:18:28.331900Z

ERROR in (postgresql-issue-136) (QueryExecutorImpl.java:2510)
Uncaught exception, not in assertion.
expected: nil
  actual: org.postgresql.util.PSQLException: ERROR: there is no parameter $1
  Position: 25
 at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse (QueryExecutorImpl.java:2510)
    org.postgresql.core.v3.QueryExecutorImpl.processResults (QueryExecutorImpl.java:2245)
    org.postgresql.core.v3.QueryExecutorImpl.execute (QueryExecutorImpl.java:311)
    org.postgresql.jdbc.PgStatement.executeInternal (PgStatement.java:447)
    org.postgresql.jdbc.PgStatement.execute (PgStatement.java:368)
    org.postgresql.jdbc.PgPreparedStatement.executeWithFlags (PgPreparedStatement.java:159)
    org.postgresql.jdbc.PgPreparedStatement.execute (PgPreparedStatement.java:148)
    next.jdbc.result_set$stmt__GT_result_set.invokeStatic (result_set.clj:631)

seancorfield 2020-07-24T07:07:02.334700Z

@nfedyashev After doing some research on this, it seems that the COPY command cannot have parameters (I found a node.js repo for PG that supports COPY and has closed out several issues asking for parameterized copy queries, saying that PG simply doesn't support it. I wondered if you could do it via EXECUTE format(...) USING ... but that doesn't seem to support the COPY command (only basic CRUD operations). So I'm not sure that this is actually possible.

seancorfield 2020-07-24T07:07:29.335500Z

Do you have links to documentation indicating that it should be possible via a PreparedStatement?

nick 2020-07-24T07:07:32.335600Z

yes, I was just trying to post it in the issue - https://www.postgresql.org/docs/9.3/sql-prepare.html

nick 2020-07-24T07:07:42.335900Z

"Any SELECT, INSERT, UPDATE, DELETE, or VALUES statement."

nick 2020-07-24T07:08:23.336600Z

I'll try to transform it into a regular string

seancorfield 2020-07-24T07:08:33.336800Z

Right, but COPY is not a SELECT, INSERT, UPDATE, DELETE

nick 2020-07-24T07:08:39.337Z

yep

seancorfield 2020-07-24T07:10:27.337400Z

So... close the issue out as "not possible"?

nick 2020-07-24T07:15:16.337600Z

yep. Thank you

nick 2020-07-24T07:16:58.339100Z

As my PostgreSQL friend explained, same issue is with TRUNCATE, GRANT functions and some others. They function more like a utility rather than other regular commands so it's kind of hard to transform them into a prepared statement

bartuka 2020-07-24T13:18:33.342200Z

hi, anyone has experience in hugsql? I need to convert this query into their syntax.

SELECT id
FROM (VALUES(4),(5),(6)) V(id)
EXCEPT
SELECT id 
FROM images;
The closer I got was
select id from (values (:v*:my-arguments)) v(id) except select id from images;
the problem is that (:v*:my-arguments) does produce (4,5,6) not (4) (5) (6)

curtis.summers 2020-07-24T15:09:07.344200Z

@iagwanderson Take a look at the Tuple List parameter type: https://www.hugsql.org/#param-tuple-list

✔️ 1
seancorfield 2020-07-24T16:22:07.344900Z

@iagwanderson Also, there's a #hugsql channel.

bartuka 2020-07-24T16:23:17.345700Z

thanks @seancorfield I will check there too next time.

seancorfield 2020-07-24T16:24:23.346500Z

Curtis is often pretty attentive here but you'll find more HugSQL users in that channel, in case he's not around 🙂

seancorfield 2020-07-24T16:24:54.347100Z

(my experience of HugSQL extends solely to getting next.jdbc working with it, per the "Getting Started with HugSQL" section of the next.jdbc docs)