Is there an equivalent to Oracles CQN in PostgreSQL? - postgresql

we have to convert an Ooracle database to PostgreSQL. Our database is using triggers and CQN (Continous Query Notification).
Triggers should be save. But we need a functionality in PostgreSQL which mimics CQN.
We use CQN to call a PL/SQL procedure after a commit of a table. In CQN you can define a query, which will be called after a commit. When this query returns a result, then the PL/SQL procedure will be called.
Is there a way to call a procedure in PostgreSQL, when the content of a defined table changed in a transaction?

Sounds like this feature from postgres would look like CQN, although it's definitely not calling SPs(and the transactional MVCC model of postgres means it'd be potentially dangerous to do so)
postgresql listen

Related

How to synchronise a foreign table with a local table?

I'm using the Oracle foreign data wrapper and would like to have local copies of some of my foreign tables locally. Is there another option than having materialized views and refreshing them manually?
Not really, unless you want to add functionality in Oracle:
If you add a trigger on the Oracle table that records all data modifications in another table, you could define a foreign table on that table. Then you can regularly run a function in PostgreSQL that takes the changes since you checked last time and applies them to a PostgreSQL table.
If you understand how “materialized view logs” work in Oracle (I don't, and I think the documentation doesn't tell), you could define a foreign table on that and use it like above. That might be cheaper.
Both of these ideas would still require you to regularly run something in PostgreSQL, but you might be cheaper. Perhaps (if you have the money) you could use Oracle Heterogenous Services to modify a PostgreSQL table whenever something changes in an Oracle table.

Execute function returning data in remote PostgreSQL database from local PostgreSQL database

Postgres version: 9.3.4
I have the need to execute a function which resides in a remote database. The function returns a table of statistic data based on the parameters given.
I am in effect only mirroring the function in my local database to lock down access to this function using my database roles and grants.
I have found the following which seem to only provide table-based access.
http://www.postgresql.org/docs/9.3/static/postgres-fdw.html
http://multicorn.org/foreign-data-wrappers/#idsqlalchemy-foreign-data-wrapper
First question: is that correct or are there ways to use these libraries for non-table based operations?
I have found the following which seems to provide me with any SQL operation on the foreign database. The negative seems to be increased complexity and reduced performance due to manual connection and error handling.
http://www.postgresql.org/docs/9.3/static/dblink.html
Second question: are these assumptions correct, and are there any ways to bypass these concerns or libraries/samples one can begin from?
The fdw interface provides a way to make a library which can allow a postgresql database to query any external data source as though it was a table. From that point of view, it could do what you want.
The inbuilt postgresql_fdw driver, however, does not allow you to specify a function as a remote table.
You could write your own fdw driver, possibly using the multicorn library, or some other language. That is likely to be a bit of work though, and would have some specific disadvantages, in particular I don't know how you would pass parameters to the function.
dblink is probably going to be the easiest solution. It allows you to execute arbitrary SQL on the remote server, returning a set of records.
SELECT *
FROM dblink('dbname=mydb', 'SELECT * FROM thefunction(1,2,3)')
AS t1(col1 INTEGER, col2 INTEGER);
There are other potential solutions but they would all be more effort to set up.

Does postgresql have Database-level triggers?

I want to build a trigger in postgresql that will fire when the server starts.
In Oracle I can use
CREATE TRIGGER iii AFTER STARTUP ON DATABASE
No. Postgres triggers can only fire when a query is run against a table
As a kludge, you might be able to find a table that's modified in certain way at database startup (perhaps there's an INSERT to some system table?) but depending on this would be a hack.

jpql sql in the same transaction

Actually i am facing a problem calling a stored procedure and making some changes in the database in the same transaction. What i am doing is that i insert some data from en EJB (3.0) using jpql into an oracle database and than i call a stored procedure with a native jpa query to make some processing with the fresh data. But the problem is that the PL/SQL function doesn't see changes unless i commit the transaction and than i make the call what i don't want to do because i want to keep all changes in the same transaction. So the question is : is there any way to insert my data, call the pl/sql function and commit everything after that (or eventually roll back all changes) ?
thank you for your help
Make sure to call entityManager.flush() before executing your stored procedure. Else, the persistence context might still have pending changes in memory. Flushing makes sure all the pending changes are written to the database.
If that doesn't work, then it means that the stored proc uses a different transaction than the one used by JPA.

Writing scripts for PostgreSQL to update database?

I need to write an update script that will check to see if certain tables, indexes, etc. exist in the database, and if not, create them. I've been unable to figure out how to do these checks, as I keep getting Syntax Error at IF messages when I type them into a query window in PgAdmin.
Do I have to do something like write a stored procedure in the public schema that does these updates using Pl/pgSQL and execute it to make the updates? Hopefully, I can just write a script that I can run without creating extra database objects to get the job done.
If you are on PostgreSQL 9.1, you can use CREATE TABLE ... IF NOT EXISTS
On 9.0 you can wrap your IF condition code into a DO block: http://www.postgresql.org/docs/current/static/sql-do.html
For anything before that, you will have to write a function to achieve what you want.
Have you looked into pg_tables?
select * from pg_tables;
This will return (among other things) the schemas and tables that exist in the database. Without knowing more of what you're looking for, this seems like a reasonable place to start.