Ecto.Repo.insert! with "on_conflict: :nothing" raising a Postgresql "syntax error at or near "ON"" - postgresql

I'm totally new to Elixir and Phoenix, and am currently working through the book "Programming Phoenix."
I've reached chapter 7 and I have a database table called categories with a column name and a DB-level uniqueness constraint on name.
If I run the following line when a category called "Hello" already exists, I get an Ecto.ConstraintError as expected:
> Rumbl.Repo.insert!(%Rumbl.Multimedia.Category{name: "Hello"})
[debug] QUERY ERROR db=3.2ms queue=4.9ms idle=9982.9ms
INSERT INTO "categories" ("name","inserted_at","updated_at") VALUES ($1,$2,$3) RETURNING "id" ["Hello", ~N[2020-04-18 07:05:04], ~N[2020-04-18 07:05:04]]
** (Ecto.ConstraintError) constraint error when attempting to insert struct:
* categories_name_index (unique_constraint)
Now, the book tells me that I can add the option on_conflict: :nothing to my call to insert! and it will prevent an error from being raised. But what actually happens is I get a postgres syntax error:
> Rumbl.Repo.insert!(%Rumbl.Multimedia.Category{name: "Hello"}, on_conflict: :nothing)
** (Postgrex.Error) ERROR 42601 (syntax_error) syntax error at or near "ON"
query: INSERT INTO "categories" ("name","inserted_at","updated_at") VALUES ($1,$2,$3) ON CONFLICT DO NOTHING RETURNING "id"
(ecto_sql) lib/ecto/adapters/sql.ex:612: Ecto.Adapters.SQL.raise_sql_call_error/1
(ecto) lib/ecto/repo/schema.ex:657: Ecto.Repo.Schema.apply/4
(ecto) lib/ecto/repo/schema.ex:263: anonymous fn/15 in Ecto.Repo.Schema.do_insert/4
(ecto) lib/ecto/repo/schema.ex:164: Ecto.Repo.Schema.insert!/4
[debug] QUERY ERROR db=0.0ms queue=0.8ms idle=9038.9ms
INSERT INTO "categories" ("name","inserted_at","updated_at") VALUES ($1,$2,$3) ON CONFLICT DO NOTHING RETURNING "id" ["Hello", ~N[2020-04-18 07:05:13], ~N[2020-04-18 07:05:13]]
Version numbers:
Elixir 1.9.4
Phoenix 1.4.10
Ecto 3.4.2
Postgres 12.1

Turns out I was mistaken that my postgres version is 12.1, this is actually the installed version of my psql client and not my postgres server, which was running 9.4. ON CONFLICT was added in 9.5, so the solution is to upgrade my postgres server.

Related

Is it possible to call a function/procedure in postgres without braces?

I am working on DB migration from Oracle to Postgres and came across a situation where a legacy Java code is calling a stored procedure from Oracle. It is a simple call without parentheses like this : Mgr.AdmSP.myproc = ad.ctlpk.proc
where ad is schema name, ctlpk is Package name and proc is Stored Procedure in Oracle
we have migrated procedure proc to Postgres but when it comes to calling, Postgres expects () for procedure or function calls even if it does not take any parameters.
So we tried to run it like this : Mgr.AdmSP.myproc = ad_ctlpk.proc(). Here ad_ctlpk is postgres schema name. After executing we are getting error : syntax error at or near "(".
Since this is a database migration only, we are not expected to make any changes to other components except database objects. Is there a way to mimic the Oracle like procedure calling style in Postgres?
Like Postgres have function now() and current_timestamp which perform same functionality but with different syntax. I am using Postgres Ver 11.11.
Adding some details as per comments for more details. In Oracle, it is defined as FUNCTION Proc RETURN INT and as of now we converted it into Postgres function as
CREATE FUNCTION ad_ctlpk.proc() RETURNS INTEGER
I am not sure about the Framework and other java code details since I dont have access to Java code. We have access to executables only, which when executed gives below error
[[AdmThread 1] [WARN:AbstractThreadedPublisherManager$ExceptionHandlingThreadGroup:48] Error in thread AdmThread 1 : Problem in doWork
Caused by: Problem running proc procedures
Caused by: ERROR: syntax error at or near "("
Position: 38, State: 42601, ErrorCode: 0
com.ubs.datait.common.distribution.exception.DistributionRuntimeException: Problem in doWork
at common.distribution.manager.AdmThread.doWork(AdmThread.java:72)
at common.distribution.common.AbstractDistributionThread.run(AbstractDistributionThread.java:30)
Caused by: common.distribution.exception.DistributionRuntimeException: Problem running proc procedures
at common.distribution.manager.AdmThread.runAdminProc(AdmThread.java:100)
at common.distribution.manager.AdmThread.doWork(AdmThread.java:67)
... 1 more
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "("
Position: 38
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2552)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2284)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:322)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:481)
Update: :
Since Postgres does not support creating/calling functions without braces (), we finally had to ask for changes in java code to add () to the function calls where required.

DB2 encrypt field in select statement

So I need to do a select statement, while encrypting a field in SHA2_512. I'm not sure which DB2 version it is, but I tried both queries and neither work.
When trying this query:
SELECT HEX(HASH_SHA256('ABCDEFGHIJKLMNOPQRZTUVWXYZ'))
FROM SYSIBM.SYSDUMMY1;
I get the following error:
Error: DB2 SQL Error: SQLCODE=-440, SQLSTATE=42884,
SQLERRMC=FUNCTION;HASH_SHA256, DRIVER=3.71.22 SQLState: 42884
ErrorCode: -440 Error: DB2 SQL Error: SQLCODE=-514, SQLSTATE=26501,
SQLERRMC=SQL_CURLH200C1, DRIVER=3.71.22 SQLState: 26501 ErrorCode:
-514
While with this query
SELECT hash('Charlie at IBM', 3)
FROM SYSIBM.SYSDUMMYU;
Error: DB2 SQL Error: SQLCODE=-440, SQLSTATE=42884,
SQLERRMC=FUNCTION;HASH, DRIVER=3.71.22 SQLState: 42884 ErrorCode:
-440 Error: DB2 SQL Error: SQLCODE=-514, SQLSTATE=26501, SQLERRMC=SQL_CURLH200C1, DRIVER=3.71.22 SQLState: 26501 ErrorCode:
-514
(while without the hash functions the query runs without any issues). Anyone know how I can fix this issue?
I'm not sure which DB2 version I'm using but by using these queries I get these results:
SELECT GETVARIABLE('SYSIBM.VERSION') FROM SYSIBM.SYSDUMMY1
DSN11015
select GETVARIABLE('SYSIBM.NEWFUN') AS COMPATIBILITY from sysibm.sysdummy1
COMPATIBILITY: V11
https://www.ibm.com/docs/en/db2/11.1?topic=functions-hash
https://www.ibm.com/docs/en/db2-for-zos/12?topic=sf-hash-crc32-hash-md5-hash-sha1-hash-sha256
You appear to be using Db2-v11 for Z/OS "DSN11015" in V11 compatibility mode.
This version lacks the scalar function HASH_SHA256 (and others) which is present in Db2-v12 for z/OS. That is the reason you get sqlcode -440 (no such function HASH...) with your Db2-for-z/os version.
At Version-11 , IBM documents a function ENCRYPT_TDES which may help you.
Background information on using this function.
You can write your own functions if you have the skills.

Liquibase Postgres error when creating hstore index

I have the following part of Liquibase changeset that creates index on a hstore column.
CREATE INDEX index_name ON ${schemaName}.table_name ((attributes->'dataset'));
When I run it I get the following error:
... 7 common frames omitted
Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: public.hstore -> unknown
Hint: No operator matches the given name and argument types. You might need to add explicit type casts.
Position: 109
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2553)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2285)
The hstore extensions is installed as I've checked it.
The engine version is 12.5 on AWS RDS instance.
This was written for postgres 9.6, so I believe it should work without any issues when it comes to combatibility.
Jdbc driver for postgres is in version of 42.2.18

Why is liquibase deleting databasechangelog rows and trying to create a renamed database table?

I am using postgres 10.5 and liquibase 3.6.2 on a Mac.
I nuke & re-create my database, run liquibase update, and it works.
But a second liquibase update fails with an exception that the pkey already exists.
After the first liquibase update, the databasechangelog table contains 97 entries. After the second, it contains 10, and the time and deployment ids for those are different than they were after the first update!
Table foo was created in an early change.
Later it was changed to be named bar, but the pkey is still foo.pkey.
Liquibase-update should not be trying to re-create foo, but it does, and fails because foo.pkey already exists.
A) In general, how can I get liquibase to output more info about what it's doing? I tried both of the commands:
liquibase --logLevel=debug --logFile=`pwd`/foo.log update
liquibase --logLevel debug --logFile `pwd`/foo.log update
Both seem to work the same, and foo.log isn't created and there's no more output in the terminal.
B) How can I stop liquibase from trying to re-make this and nuking my databasechangelog?
I tried to make a small example that fails, but this seems to work... Others here are using it with postgres 9.5.10 with no problem...
All I see in the terminal is:
Starting Liquibase at Wed, 14 Nov 2018 13:06:44 PST (version 3.6.2 built at 2018-07-03 11:28:09)
Unexpected error running Liquibase: ERROR: relation "cant_change_pkey" already exists [Failed SQL: CREATE TABLE nuss.cant_change (message_id UUID NOT NULL, origin VARCHAR(4), type VARCHAR(12) NOT NULL, CONSTRAINT CANT_CHANGE_PKEY PRIMARY KEY (message_id), UNIQUE (message_id))]
liquibase.exception.MigrationFailedException: Migration failed for change set db/changelog/changelog-new1.xml::first-one::rstrauss:
Reason: liquibase.exception.DatabaseException: ERROR: relation "cant_change_pkey" already exists [Failed SQL: CREATE TABLE nuss.cant_change (message_id UUID NOT NULL, origin VARCHAR(4), type VARCHAR(12) NOT NULL, CONSTRAINT CANT_CHANGE_PKEY PRIMARY KEY (message_id), UNIQUE (message_id))]
at liquibase.changelog.ChangeSet.execute(ChangeSet.java:637)
at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:53)
at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:78)
at liquibase.Liquibase.update(Liquibase.java:202)
at liquibase.Liquibase.update(Liquibase.java:179)
at liquibase.integration.commandline.Main.doMigration(Main.java:1205)
at liquibase.integration.commandline.Main.run(Main.java:191)
at liquibase.integration.commandline.Main.main(Main.java:129)
Caused by: liquibase.exception.DatabaseException: ERROR: relation "cant_change_pkey" already exists [Failed SQL: CREATE TABLE nuss.cant_change (message_id UUID NOT NULL, origin VARCHAR(4), type VARCHAR(12) NOT NULL, CONSTRAINT CANT_CHANGE_PKEY PRIMARY KEY (message_id), UNIQUE (message_id))]
at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:356)
at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:57)
at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:125)
at liquibase.database.AbstractJdbcDatabase.execute(AbstractJdbcDatabase.java:1229)
at liquibase.database.AbstractJdbcDatabase.executeStatements(AbstractJdbcDatabase.java:1211)
at liquibase.changelog.ChangeSet.execute(ChangeSet.java:600)
... 7 common frames omitted
Caused by: org.postgresql.util.PSQLException: ERROR: relation "cant_change_pkey" already exists
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2476)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2189)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:300)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:428)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:354)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:301)
at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:287)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:264)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:260)
at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:352)
... 12 common frames omitted
For more information, please use the --logLevel flag

Attempted to delete invisible tuple + Postgres

I have system where we perform large number of inserts and updates query(something upsert as well)
I see occasionally error on my logs that states ..
PG::ObjectNotInPrerequisiteState: ERROR: attempted to delete invisible tuple
INSERT INTO call_records(plain_crn,efd,acd,slt,slr,ror,raw_processing_data,parsed_json,timestamp,active,created_at,updated_at) VALUES (9873,2016030233,'R',0,0,'PKC01','\x02000086000181f9000101007 ... ')
What I fail to understand even when no (delete) query is performed (the above error appear on insert clause) yet the error was thrown.
I have been googling around this issue but no conclusive evidence of why this happen.
Version of Postgres.
database=# select version();
version
--------------------------------------------------------------------------------------------------------------
PostgreSQL 9.5.2 on x86_64-apple-darwin14.5.0, compiled by Apple LLVM version 7.0.0 (clang-700.1.76), 64-bit
(1 row)
Any Clue ??