Postgresql functions execution process - postgresql

I have a procedural function (written in pl/python) which queries table A, does some calculations and then returns a set. I use this function as query for my materialized view B.
Everything works perfectly except that when I want to restore my dump, I get the following error:
DETAIL: spiexceptions.UndefinedTable: relation "A" does not exist.
The line which raises this error is the last line of my sql dump:
REFRESH MATERIALIZED VIEW B;
I know that I can ignore this error and refresh my materialized view after restoration process, but I want to know why this happens? Is it because this function runs in another transaction which doesn't know anything about current restoration process? And what can I do to prevent this error?

For security reasons, pg_dump (or pg_restore) emits a command which empties the search_path, so when you restore the process gets run with an empty search path. But it does not edit the text body of your function at all but emits it as-is, so it can't alter it to specify the fully qualified name of the table. So the function can't find the table when run inside the process doing the restore.
You can fully qualify the table name in the function, or you can define the function with SET search_path = public. Or you can edit the dump file to remove the part that clears the search_path, if you are not concerned about the security implications.

Related

SQL Server Query in the debug mode: querying a table?

I have a large script that I need to debug to catch an error. There is a table in the script declared as a variable. Some t-sql makes an insert into that table. I would like to select from the table while in the debug mode. I have the "locals" window opened on the screen but I cannot see the contents of the table there, just the variable itself, neither can I select from the temporary table-variable when the code execution stops at the desired breakpoint.
Is there a way I can query the table in the debug mode? Thanks!
For the purposes of debugging, you could replace the table variable (#tableName) with a global temporary table (##tableName).
Table variables and local temporary tables (#tableName) only exist within the session where they're defined, so can only be queried within that session. Global temporary tables can be accessed from other sessions and will persist until all connections to them are dropped, so you'll be able to check results from a different SSMS window as the script is executing in it's window.
You'll want to comment out the table variable definition, then add the CREATE TABLE ##... statement. After that, Find & Replace should get your script ready (and put it back when you're done).
Here's the documentation on Temporary Tables.

REORG command in db2

So I have been altering a table in QMF. After 3 alters I believe the table has gone into a pending reorg state so that I cannot alter it additionally. Am I correct in this assumption? If so what implications does this have and to get around it can I simply reorganize the table and continue altering it? If so, what does the syntax look like for reorganizing a table? I tried
REORG TABLE PIDJBIP.TABLE_NAME_T
and receive the error:
an unexpected token "PIDJBIP" was found following "REORG TABLE".
Expected tokens may include: "JOIN". SQL state = 42601.
I haven't gotten much help out of the IBM pages regarding this subject.
REORG is not an SQL statement, so it cannot be issued using a SQL interface (such as QMF). You will need to run it using the DB2 Command Line Processor.
Alternatively, you might use the administrative stored procedure, which you could call via QMF:
call sysproc.admin_cmd('reorg table PIDJBIP.TABLE_NAME_T')

How to View Execution Plan for Query Containing a Temp Table in Toad for SQL Server?

I am trying to tune the performance of a stored procedure that contains a temp table in Toad for SQL Server. After selecting "Include Actual Execution Plan" from the 'Editor' menu, I run the query. The Results Set returns values as expected, however, the Execution Plan tab shows the following error:
Invalid object name '#temp'.
I have tried creating the temp tables first then just executing the SELECT statement that references it, I tried creating the temp tables as global temp tables and running the SELECT statement in another window, and I have messed with the SHOWPLAN_TEXT and STATISTICS PROFILE (as mentioned in this question) but I keep receiving the same error. The only thing I have not tried is using a table variable, but the changes I will be making cannot be done on table variables, so this is not really an option for me at this time.
Has anyone else come across this or have any ideas as to what I might be doing wrong?
You'll want to use the ISQL command line utility on a machine that has SQL Server client package installed. Or any other utility that can submit a query to SQL Server.
ISQL Docs and How to get an execution plan (2nd part of the post)

postgres condition trigger missing from clause

I'm trying to create a trigger on my table such that it only runs if the 'prepaid' column is true for rows where I've modified the value of the 'points_per_month' column. I tried this:
CREATE TRIGGER "fix_usage_trigger"
AFTER UPDATE OF "points_per_month"
ON "public"."clients"
FOR EACH ROW WHEN (ROW.prepaid)
EXECUTE PROCEDURE "fix_prepaid_client_available_usage"();
psql is telling me this:
ERROR: missing FROM-clause entry for table "row"
LINE 1: ...r_month" ON "public"."clients" FOR EACH ROW WHEN (ROW.prepai...
Clearly I have no FROM clause there, but I'm not sure why I'd need one, nor where to put it.
That should be when (new.prepaid), per David's comment. You can access old and new in the when clause (as in the row before and after the update) much like table aliases. The error message is PG complaining that row is not a known table.
Two additional notes:
it might need to be when (old.prepaid or new.prepaid) if you want to manage billing plan switches -- or another two separate triggers. Conversely, when (old.prepaid and new.prepaid) if you do not, and someone might run database queries that might inadvertently fire the trigger and create undesirable state (add a unit test or two).
the function's name suggest something might be wrong further up in your code flow. You might want to fix that instead, by setting the available usage properly to begin with. Doing so might be more efficient, too.

Incorrect syntax near the keyword 'truncate'. within a View

I get the error "Incorrect syntax near the keyword 'truncate'." and not sure what's wrong with the syntax here, it's not obvious to me...probably something stupid but I need another set of eyes:
ALTER VIEW [dbo].[vw_All_Events]
AS
truncate table Event
Select [event_id]
,[site_id]
,[autogenerated]
,[creation_date]
,[last_update_date]
from Event
GO
A view only allows a single statement after the AS and it must be data retrieval (return a rowset). It can't be a different type including data definition, data modification, procedural, declarative, or any other.
You can do these things with a stored procedure, or a user-defined function (but can't do DDL & DML in a function).
In detail, a stored procedure allows flow-of-control statements like IF THEN ELSE BEGIN END WHILE RETURN. You can use DML to UPDATE, DELETE and INSERT. You can use DDL to CREATE and DROP tables and indexes, add columns and constraints, and so on. You can return multiple rowsets. You can execute dynamic SQL.
What are you trying to accomplish?
Better way, you use a stored procedure instead of view.
There are you write multiple statement and also get output.
You can only have select statements in views. Therefore, 'truncate' is an invalid command to use.
TRUNCATE does not work with views.
Check out this link