Can I notify and listen inside PostgreSQL procedures (functions)? - postgresql

I have checked the documentation (for my version 9.3):
http://www.postgresql.org/docs/9.3/static/sql-notify.html
http://www.postgresql.org/docs/9.3/static/sql-listen.html
I have read multiple discussions and blogs about notify-listen in postgres.
They all use a listening process / interface, which is not implemented inside "classic" procedure (which is function in postgres anyway). They implement it in different language and/or environment, external to the postgres server (e.g. perl, C#).
My question: Is it possible to implement listen(ing) inside postgres function (language plpgsql) ? If not (what I assume from not being to able to find such topic / example), can someone explain a bit, why it can't be done, or maybe why it does not make sense to do it that way ?

It is a classic use case for Trigger Function in case you depend on a single table: https://www.postgresql.org/docs/current/plpgsql-trigger.html

Related

Atomic Operations with MongoDB + MongoCXX

I am new to MongoDB, but experienced with C++. I am using the MongoCXX driver within my application. What I need to do now is to increment a counter shared by multiple programs. My thought was that this requires an atomic operation, but I do not see how to do this with MongoCXX.
Searching, I have found that it appears as though a legacy, deprecated API has exactly what I need:
http://mongocxx.org/api/legacy-v1/atomic__word__intrinsics_8h_source.html
In here, I see exactly what I want to do:
WordType fetchAndAdd(WordType increment)
However, I do not see anything like this in the current C++ driver.
http://mongocxx.org/api/mongocxx-3.6.2/annotated.html
Is there any way to do this type of operation with the current API that I am missing?
You can use something like https://docs.mongodb.com/manual/reference/operator/update/inc/.
Some drivers provide wrappers for server commands, but such wrappers aren't strictly necessary because you can always invoke the server commands directly.

How to get column name and data type returned by a custom query in postgres?

How to get column name and data type returned by a custom query in postgres? We have inbuilt functions for table/views but not for custom queries. For more clarification I would say that I need a postgres function which will take sql string as parameter and will return colnames and their datatype.
I don't think there's any built-in SQL function which does this for you.
If you want to do this purely at the SQL level, the simplest and cheapest way is probably to CREATE TEMP VIEW AS (<your_query>), dig the column definitions out of the catalog tables, and drop the view when you're done. However, this can have a non-trivial overhead depending on how often you do it (as it needs to write view definitions to the catalogs), can't be run in a read-only transaction, and can't be done on a standby server.
The ideal solution, if it fits your use case, is to build a prepared query on the client side, and make use of the metadata returned by the server (in the form of a RowDescription message passed as part of the query protocol). Unfortunately, this depends very much on which client library you're using, and how much of this information it chooses to expose. For example, libpq will give you access to everything, whereas the JDBC driver limits you to the public methods on its ResultSetMetadata object (though you could probably pull more information from its private fields via reflection, if you're determined enough).
If you want a read-only, low-overhead, client-independent solution, then you could also write a server-side C function to prepare and describe the query via SPI. Writing and building C functions comes with a bit of a learning curve, but you can find numerous examples on PGXN, or within Postgres' own contrib modules.

PostgreSQL V9.4 - Logical Decoding - SQL interface: Starting at a specific LSN?

I use PostgreSQL Logical Decoding functionality for retrieving WAL contents:
SELECT * FROM pg_create_logical_replication_slot(...)
SELECT * FROM pg_logical_slot_get_changes('<my_slot>', NULL, NULL);
These calls are implemented in the SQL interface within a "C" / ODBC
programming framework.
This works very nice for me.
Data records are fetched one by one in a row - as expected.
Yet - there are certain cases where I need to re-position the slot flow
to a past LSN.
The Logical Decoding REPLICATION interface provides means for achieving that.
For example the PG_RECVLOGICAL program introduces a "--startpos=X/Y" option
that works as I expect.
Is there an equivalent option in the "SQL" interface?
I guess that both interfaces share much in common.
So - I went through the documentation and did not manage to find any equivalent
option in the SQL interface.
For now - the SQL interface moves forward only and cannot retract back to a
past LSN.
Did I miss something here?
Does any body have any clue or experience with that?
Kindest regards
Hillel.
According to the replication SQL function docs it doesn't look possible :-(
http://www.postgresql.org/docs/9.4/static/functions-admin.html#FUNCTIONS-REPLICATION

JavaM API for GT.M - SELECT support

I'm wondering if there's any possible way how to use or implement SELECT query into JavaM API for GT.M database system. I'm using version 0.1, since I haven't found any other version ( https://github.com/Gadreel/javam/blob/master/README.md ).
If there's no option yet, could you recommend me any other API for this DBMS, using Java? I know there's some gtm4j ( http://code.vistaehr.com/gtm4j ), but it takes advantage of springframework, which is not convenient for me.
I'm new with GT.M and I just want to test, how to connect to it using Java and use some basic queries. Thanks a lot for your advices.
The database side of GT.M is a hierarchical key-value store, so features like SELECT (I'm guessing you want a full SQL SELECT) needs to be implemented by some framework (either an existing framework or one created by you).
From a quick look at the JavaM API, it seems it only offers/showcase a Java interface to the features offered by GT.M. So I think you would have to implement the SQL SELECT feature yourself, in Java.
That said, it is possible that what you wanted to use a SQL SELECT for can be done easilly using the standard GT.M / JavaM API, so there would be no need to implement a full SQL SELECT.
Actually, you could use M to write a parser for your SELECT command syntax. And would certainly be easier to do with the GTMJI plug-in for full-duplex GT.M/Java communication that FIS have just released.

Namespaces in Redis?

Is it possible to create namespaces in Redis?
From what I found, all the global commands (count, delete all) work on all the objects. Is there a way to create sub-spaces such that these commands will be limited in context?
I don't want to set up different Redis servers for this purpose.
I assume the answer is "No", and wonder why wasn't this implemented, as it seems to be a useful feature without too much overhead.
A Redis server can handle multiple databases... which are numbered. I think it provides 32 of them by default; you can access them using the -n option to the redis-cli shell scripting command and by similar options to the connection arguments or using the "select()" method on its connection objects. (In this case .select() is the method name for the Python Redis module ... I presume it's named similarly for other libraries and interfaces.
There's an option to control how many separate databases you want in the configuration file for the Redis server daemon as well. I don't know what the upper limit would be and there doesn't seem to be a way to dynamically change that (in other words it seems that you'd have to shutdown and restart the server to add additional DBs). Also, there doesn't seem to be an away to associate these DB numbers with any sort of name nor to impose separate ACLS, nor even different passwords, to them. Redis, of course, is schema-less as well.
If you are using Node, ioredis has transparent key prefixing, which works by having the client prepend a given string to each key in a command. It works in the same way that Ruby's redis-namespace does. This client-side approach still puts all your keys into the same database, but at least you add some structure, and you don't have to use multiple databases or servers.
var fooRedis = new Redis({ keyPrefix: 'foo:' });
fooRedis.set('bar', 'baz'); // Actually sends SET foo:bar baz
If you use Ruby you can look at these gems:
https://github.com/resque/redis-namespace
https://github.com/jodosha/redis-store