Friday 9 November 2018

What's the equivalent of blackhole?

I'm starting a new project in postgreSQL (lastest) having worked with MySQL 5.5 for a while.
In the past I've made heavy use of blackhole table to simplify my application code.
It allows me to do one insert in application code:
INSERT INTO blackhole1 (val1, val2, val3, val4 ...

CREATE TRIGGER ai_blackhole1_each AFTER INSERT ....
BEGIN
  INSERT INTO table1 (....
  INSERT INTO table2 (....
  INSERT INTO log (.....
And have a trigger in the blackhole table insert the values into different tables.
What do I use in postgreSQL to replace this functionality?
I know I can use a stored procedure, but that means that I cannot connect data-aware controls to a 
blackhole-table. So I would love the stick as close to the MySQL original as possible.

 Answers


What is a BLACKHOLE?
In MySQL-speak, BLACKHOLE is a storage engine that simply discards all data INSERTed into it, analogous to a null device. There are a number of reasons to use this backend, but they tend to be a bit abstruse:
  • A "relay-only" binlog-filtering slave
    See the docs, and here.
  • Benchmarking
    E.g., measuring the overhead of binary logging without worrying about storage engine overhead
If you don't know why you need a data sink masquerading as a table, don't use it.

What is the technique you are asking about?

The use under consideration seems to be to:
  1. redirect INSERTed data to other tables
  2. audit log the original INSERTion action
  3. discard the original INSERT data
Thus the answer to the question of "evilness" or pros/cons is the same as the answer to those questions for insertable/updatable VIEWs (the common way to implement #1), trigger-based audit logging (how most people do #2) and behavioral overrides/counteractions generally (there are a number of ways to accomplish #3).

So, what is the answer?

The answer is, of course, "sometimes these techniques are appropriate and sometimes not." :) Do you know why you're doing it? Is the application a better place for this functionality? Is the abstraction too brittle, too leaky, too rigid, etc.?



inserting in different tables through one mysql query

Short answer
No you can't insert into more than one table using a single SQL statement.
Longer answer
You can achieve it by inserting into table1 and then setting up a trigger on table1 to insert data into tables 2 and 3 using a BLACKHOLE table to store your data transiently. 
Personally I would avoid this like the plague since it obscures how & where data is being inserted into the database from a given application. 
If I were you I would just write three separate INSERT statements in your php application and avoid trying to create a single query that inserts into multiple tables. It'll save you a great deal of pain!



As far as I know, PHP's mysql_query() function doesn't support multiple queries (insert, select, update etc), and as you are inserting into different tables with different fields/columns, you have to run a separate query for each. I don't think there is a way to do what you want.

0 comments:

Post a Comment