How does create directory if it doesn't exist in PL/pgsql (postgresql)? - windows

I defined a trigger in postgresql as follow:
CREATE OR REPLACE FUNCTION test() RETURNS trigger AS
$BODY$
BEGIN
COPY (select 1) TO PROGRAM 'mkdir -m 700 -p mfdata';
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql;
Now I use this trigger when a record of table aircraft updated:
CREATE TRIGGER aircraft_test AFTER UPDATE ON aircraft
FOR EACH ROW EXECUTE PROCEDURE test();
This means when a record in aircraft table updated, mfdata directory created if not exist in postgres data directory. First record update, it create a folder whit mfdata name, but in second update record, it error:
for when doesn't create the folder if it exist, I'm using -p, but it error.
Why does it error? How can I fixe it?
Note:
I want to create sub folder dynamically later.
DB Log:
2018-01-27 08:24:40 IRST LOG: autovacuum launcher started
A subdirectory or file -m already exists.
Error occurred while processing: -m.
A subdirectory or file 700 already exists.
Error occurred while processing: 700.
A subdirectory or file -p already exists.
Error occurred while processing: -p.
A subdirectory or file mfdata already exists.
Error occurred while processing: mfdata.
2018-01-27 17:34:49 IRST ERROR: program "mkdir -m 700 -p mfdata" failed
2018-01-27 17:34:49 IRST DETAIL: child process exited with exit code 1
2018-01-27 17:34:49 IRST CONTEXT: SQL statement "COPY (select 1) TO PROGRAM 'mkdir -m 700 -p mfdata'"
PL/pgSQL function test() line 3 at SQL statement

Related

Resolving paths in mingw fails with Data Version Control

I am following the tutorial about Data Version Control using mingw32 on Windows 7.
I am getting very strange error when I try to use run:
$ dvc run -v echo "hello"
Debug: updater is not old enough to check for updates
Debug: PRAGMA user_version;
Debug: fetched: [(2,)]
Debug: CREATE TABLE IF NOT EXISTS state (inode INTEGER PRIMARY KEY, mtime TEXT NOT NULL, md5 TEXT NOT NULL, timestamp TEXT NOT NULL)
Debug: CREATE TABLE IF NOT EXISTS state_info (count INTEGER)
Debug: CREATE TABLE IF NOT EXISTS link_state (path TEXT PRIMARY KEY, inode INTEGER NOT NULL, mtime TEXT NOT NULL)
Debug: INSERT OR IGNORE INTO state_info (count) SELECT 0 WHERE NOT EXISTS (SELECT * FROM state_info)
Debug: PRAGMA user_version = 2;
Running command:
echo hello
/c: /c: Is a directory
Debug: SELECT count from state_info WHERE rowid=1
Debug: fetched: [(1,)]
Debug: UPDATE state_info SET count = 1 WHERE rowid = 1
Error: Traceback (most recent call last):
File "dvc\command\run.py", line 18, in run
File "dvc\project.py", line 265, in run
File "dvc\stage.py", line 435, in run
StageCmdFailedError: Stage 'Dvcfile' cmd echo hello failed
Error: Failed to run command: Stage 'Dvcfile' cmd echo hello failed
Question:
Where does the /c: /c: Is a directory come from? How can I fix it?
My findings
I supposed that it was resolving path to echo, but ech is a builtin.
$ type echo
echo is a shell builtin
I tried also with exit and cd but I am getting the same error.
Calling commands without dvc works fine.
dvc with --no-exec flag works fine, but when later executed with repro gives the same error.
I'm one of the dvc developers. Similar error has affected dvc running on cygwin. We've released a fix for it in 0.20.0. Please upgrade.

Getting error - Unknown Command , while deleting view if exists in oracle

IF EXISTS(select 1 from sys.views where name='release_testcase_count')
DROP VIEW ITCC.release_testcase_count4;
i am able to delete this view but it is saying -
Error starting at line : 1 in command -
IF EXISTS(select 1 from sys.views where name='release_testcase_count')
Error report -
Unknown Command
View ITCC.RELEASE_TESTCASE_COUNT4 dropped.
so in this case 1 line have a error, it is not checking it exist or not.
IF EXISTS command is valid in SQL Server or other databases but not in ORACLE.
Your first line is completely ignored and signaled as unknown command as Oracle does not recognize any command starting with IF EXIST.
Second line is a valid command, so the view is dropped as a result.

Import Postgres dump into a table with CockroachDB

After executing a pg_dump from PostgeSQL, I attempted to import the .sql file into CockroachDB, but received the following errors:
ERROR: unknown variable: "STATEMENT_TIMEOUT"
ERROR: unknown variable: "LOCK_TIMEOUT"
ERROR: unknown variable: "IDLE_IN_TRANSACTION_SESSION_TIMEOUT"
SET
SET
ERROR: unknown variable: "CHECK_FUNCTION_BODIES"
SET
ERROR: unknown variable: "ROW_SECURITY"
SET
ERROR: unknown variable: "DEFAULT_TABLESPACE"
ERROR: unknown variable: "DEFAULT_WITH_OIDS"
CREATE TABLE
ERROR: syntax error at or near "OWNER"
Any guidance?
CockroachDB has special support for using psql, which supports the COPY command (which is faster than batched INSERT statements).
You'll need to do two things:
Clean up the SQL file
Import it into CockroachDB (which sounds like you tried, but I'll include the steps here for anyone else who needs them):
Clean up the SQL File
After generating the .sql file, you need to perform a few editing steps before importing it:
Remove all statements from the file besides the CREATE TABLE and COPY statements.
Manually add the table's PRIMARY KEY constraint to the CREATE TABLE statement.
This has to be done manually because PostgreSQL attempts to add the primary key after creating the table, but CockroachDB requires the primary key be defined upon table creation.
Review any other constraints to ensure they're properly listed on the table.
Remove any unsupported elements, such as arrays.
Import Data
After reformatting the file, you can import it through psql:
$ psql -p [port] -h [node host] -d [database] -U [user] < [file name].sql
For reference, CockroachDB uses these defaults:
[port]: 26257
[user]: root

Oracle triggers error on sqldeveloper export script

I had built a few tables with sequences and triggers, since I need to share the script with my team at uni I did an export with sqldeveloper, now when I try to import/execute the resulted .sql I'm getting errors with triggers.
This is the error message:
"Error starting at line 250 in command:
CREATE OR REPLACE EDITIONABLE TRIGGER "TRG_ACCOUNTS"
BEFORE INSERT ON ACCOUNTS
FOR EACH ROW
BEGIN
SELECT SEQ_ACCOUNTS.NEXTVAL INTO :NEW.ACCOUNT_ID FROM DUAL
Error report:
SQL Command: editionable TRIGGER
Failed: Warning: execution completed with warning
Error starting at line 258 in command:
END
Error report:
Unknown Command
trigger "TRG_ACCOUNTS" altered."
This is the part of the script it's complaining about. I have a few other triggers and it's giving me the same error on all of them. I've checked how the triggers are created after executing the script, and all of them seem to be missing a semi colon and the "END;" at the end.
Example:
create or replace
TRIGGER "TRG_FL_AR"
BEFORE INSERT ON FLOOR_AREAS
FOR EACH ROW
BEGIN
SELECT SEQ_FL_AR.NEXTVAL INTO :NEW.FLOOR_AREA_ID FROM DUAL <-- missing ";" here
<missing "END;" here>
Could you please help me?
Thank you.
Try to put a back slash / after line 258 and check again

ORA-29283: invalid file operation ORA-06512: at "SYS.UTL_FILE", line 536

Below is the code i use to extract data from a table to a flat file.
BEGIN
DECLARE
file_name VARCHAR2(50);
file_handle utl_file.file_type;
BEGIN
file_name := 'table.txt';
file_handle := utl_file.fopen('SEND',file_name,'W');
FOR rec in(
SELECT column 1
||'~'||column 2
||'~'||column 3 out_line
FROM table1)LOOP
UTL_FILE.PUT_LINE(file_handle,rec.out_line);
UTL_FILE.FFLUSH(file_handle);
END LOOP;
UTL_FILE.FCLOSE(file_handle);
END;
end;
This code is working fine in our development database but its throwing the below error if i execute in a new DB.
Error starting at line 1 in command:
BEGIN
DECLARE
file_name VARCHAR2(50);
file_handle utl_file.file_type;
BEGIN
file_name := 'table.txt';
file_handle := utl_file.fopen('SEND',file_name,'W');
FOR rec in(
SELECT column 1
||'~'||column 2
||'~'||column 3 out_line
FROM table1)LOOP
UTL_FILE.PUT_LINE(file_handle,rec.out_line);
UTL_FILE.FFLUSH(file_handle);
END LOOP;
UTL_FILE.FCLOSE(file_handle);
END;
end;
Error report:
ORA-29283: invalid file operation
ORA-06512: at "SYS.UTL_FILE", line 536
ORA-29283: invalid file operation
ORA-06512: at line 7
29283. 00000 - "invalid file operation"
*Cause: An attempt was made to read from a file or directory that does
not exist, or file or directory access was denied by the
operating system.
*Action: Verify file and directory access privileges on the file system,
and if reading, verify that the file exists.
Oracle directory 'SEND' points to some UNIX directory which has rights as
'rwxrwsr-x' (Octal 2775)
Oracle Version:11g
Please help me to solve this issue.
Guys please do let me know if you require more data from me to solve this question.
So, #Vivek has got the solution to the problem through a dialogue in the Comments rather than through an actual answer.
"The file is being created by user oracle just noticed this in our development database. i'm getting this error because, the directory where i try to create the file doesn't have write access for others and user oracle comes under others category. "
In the absence of an accepted answer to this question I proffer a link to an answer of mine on the topic of UTL_FILE.FOPEN(). Find it here.
P.S. I'm marking this answer Community Wiki, because it's not a proper answer to this question, just a redirect to somewhere else.
Assume file is already created in the predefined directory with name "table.txt"
1) change the ownership for file :
sudo chown username:username table.txt
2) change the mode of the file
sudo chmod 777 table.txt
Now, try it should work!
On Windows also check whether the file is not encrypted using EFS. I had the same problem untill I decrypted the file manualy.
I had been facing this problem for two days and I found that the directory you create in Oracle also needs to created first on your physical disk.
I didn't find this point mentioned anywhere i tried to look up the solution to this.
Example
If you created a directory, let's say, 'DB_DIR'.
CREATE OR REPLACE DIRECTORY DB_DIR AS 'E:\DB_WORKS';
Then you need to ensure that DB_WORKS exists in your E:\ drive and also file system level Read/Write permissions are available to the Oracle process.
My understanding of UTL_FILE from my experiences is given below for this kind of operation.
UTL_FILE is an object under SYS user. GRANT EXECUTE ON SYS.UTL_FILE TO
PUBLIC; needs to given while logged in as SYS. Otherwise, it will
give declaration error in procedure. Anyone can create a directory as
shown:- CREATE OR REPLACE DIRECTORY DB_DIR AS 'E:\DBWORKS'; But CREATE
DIRECTORY permission should be in place. This can be granted as
shown:- GRANT CREATE ALL DIRECTORY TO user; while logged in as SYS
user. However, if this needs to be used by another user, grants need
to be given to that user otherwise it will throw error. GRANT READ,
WRITE, EXECUTE ON DB_DIR TO user; while loggedin as the user who
created the directory. Then, compile your package. Before executing
the procedure, ensure that the Directory exists physically on your
Disk. Otherwise it will throw 'Invalid File Operation' error. (V.
IMPORTANT) Ensure that Filesystem level Read/Write permissions are in
place for the Oracle process. This is separate from the DB level
permissions granted.(V. IMPORTANT) Execute procedure. File should get
populated with the result set of your query.
The ORA-29283: invalid file operation is also raised on utl_file.put if there is an attempt to write line longer than max_linesize in text mode. max_linesize is optional 4th parameter of utl_file.fopen function defaulting to 1024.
(My case was dumping CSV from within Oracle in Docker into file in host directory mapped as Docker volume and I was misleaded by this error for pretty significat time - I seeked cause in filesystem rights or volume mapping between Docker and host, actually it was so stupid cause.)
UPDATE: another occurence of same exception also happened on utl_file.fopen. The database rejected to create file even if the file did not exist before. The directory in which the attempt of file creation happened was mapped on Docker volume. It started to work if the zero-sized file was created on host machine in advance. Attempt to create file from within container (touch /dir/file) failed though. Perhaps some docker issue - it disappeared after restarting Docker Desktop.
You need give permission by creating folder.
create or replace directory DINESH as '/home/oracle/DINESH/';
grant read, write
on directory DINESH
to public;
Simple PLSQL to open a file,
-- write two lines into the file,
-- and close the file
declare
fhandle utl_file.file_type;
begin
fhandle := utl_file.fopen(
'DINESH' -- File location
, 'test_file.txt' -- File name
, 'w' -- Open mode: w = write.
);
utl_file.put(fhandle, 'Hello world!'|| CHR(10));
utl_file.put(fhandle, 'Hello again!');
utl_file.fclose(fhandle);
exception
when others then
dbms_output.put_line('ERROR: ' || SQLCODE || ' - ' || SQLERRM);
raise;
end;
test_file.txt file created in /home/oracle/DINESH.

Resources