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.
Related
How can I (using PG-Admin) access or create a log (ideally timestamped) that displays the changes that have happened to a table?
First of the way enable pg_stat_statements on PostgreSQL.
create extension pg_stat_statements;
After then you can view all SQL queries executed on your DB. At this you can view when executed SQL, when finished, duration of execute times and etc.
For more details: PostgreSQL Documentation - pg_stat_statements
If you need history of updated or deleted records on the tables, you can do it for all tables manually writing triggers or own functions, using JSON(JSONB) data types.
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.
In one of my migration files on my development box I have this DB2 request:
CALL SYSPROC.ADMIN_CMD('REORG TABLE COST_RULES.LOW_DLL_EXCEP');
This call seems to be needed for a subsequent ALTER on a column done in a subsequent migration. In the past the devops person manually executed the call to reorg on the test database, but I'd like to put it into the migration so it gets done automatically.
If I add this, it will change the checksum on the migration file, causing a flyway issue when the deployment happens. What Flyway steps should be taken before the deployed job works?
When a table has had certain kinds of alterations, or a certain number of alterations, Db2 can put the table into reorg pending status.
When the table is NOT in reorg_pending condition, it is not necessary to preform reorg at this time solely for the purposes of migrations.
Consider changing your migration to make reorg conditional, and also consider online reorg if the table type is compatible.
You can use view SYSIBMADM.ADMINTABINFO and check REORG_PENDING='Y' for your table to decide whether or not to perform a reorg. You can use an SQL PL anonymous block to run the conditional logic and conditional reorg.
You can use the INPLACE option for reorg (and related options) if the table is suitable for online reorg.
You could also use an entirely separate migration, to test if any tables in your schema(s) of interest are in reorg_pending state, and take appropriate action at that time, including checking the table type to see if an online reorg is appropriate. Such a migration would be re-runnable. It would have its own checksum.
If you're correcting a script that has already been run, and you don't want it to run again (but you need the change either to reflect a change done manually on production or to spin up copies faithfully) then run flyway repair once the change is made - that will recalculate the checksums to align with the current state of the scripts.
I'm trying to save all ddl scripts that runs on the database in a table. In SQL Server this could be achieved by setting a DDL Trigger on the database and getting the scripts from the EVENT_DATA object in the trigger context. So far, I haven't found a way do the same thing in PostgreSQL.
Is it even possible?
Yes, it's possible save DDL commands to table, see DDL_log_to_table
rollback;
doesn't seem to undo alter table changes.
Background:
I'm generating some .sql scripts (based on parsed Hibernate scripts) which are trashing my tables. Importing the full database for testing takes up to 30 minutes (also slowing my machine) and as much as I enjoy taking breaks, i'd prefer to just undo everything with a command such as rollback and try again.
btw this is Oracle 10g Express Edition Release 10.2.0.1.0
Is this even possible?
With the express edition, I'm not sure this is possible. You cannot rollback a DDL operation like ALTER TABLE because DDL is implicitly committed.
Oracle does have the option to create restore points that you can then flashback the entire database to a point in time relatively quickly. That will undo the effects of all committed transactions (DML and DDL) between the creation of the restore point and the point where you issued the flashback command. Here is an example of creating and flashing back to a restore point and here's another that does the flashback for the entire database. I'm just not sure that this functionality is available in the express edition.
This version of Oracle performs a commit on any ALTER TABLE statements.
See this post:
it possible to roll back CREATE TABLE and ALTER TABLE statements in major SQL databases?