Skip to main content
Mod Moved Comments To Chat
Notice removed Comments only by CommunityBot
Post Unlocked by CommunityBot
Post Locked by Paul White
Notice added Comments only by Paul White
Rollback to Revision 8
Source Link
Erwin Brandstetter
  • 182.1k
  • 28
  • 457
  • 620

I have multiple tables that have the exact same column names, they only vary in their column values, like:

tbl_log_a
tbl_log_b
tbl_log_c
...

26 tables (fromfrom a to z). Each table has a Triggertrigger that calls a Trigger Function named "trfn_tbl_log_%letter% (from a to z)trigger function which does the exact same thing:

CREATE OR REPLACE FUNCTION trfn_tbl_log_a
  RETURNS trigger AS
$BODY$
DECLARE
v_timeidx real;

BEGIN

IF NEW.timetype = 'start' THEN
  SELECT timeidxcolumnname FROM tbl_log_a WHERE fnname = NEW.fnname AND timetype = 'start' ORDER BY stmtserial DESC LIMIT 1 INTO v_timeidx;
    IF FOUND THEN
      NEW.timeidx := floor(v_timeidx) + 1;
    ELSE
      NEW.timeidx := 1;
    END IF;

END IF;
return NEW;

END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION trfn_tbl_log_a
  OWNER TO postgres;

SoOther than that, all my trigger functions are exactlydo the exact same, they only thing. They differ in the table wherethat they select the columns (in its first IF STATEMENT):

select timeidxcolumnname FROM tbl_log_a WHERE fnname...
select timeidxcolumnname FROM tbl_log_b WHERE fnname...
select timeidxcolumnname FROM tbl_log_c WHERE fnname...
...

My Trigger definition script is...

CREATE TRIGGER trfn_tbl_log_a
  BEFORE INSERT
  ON tbl_log_a
  FOR EACH ROW
  EXECUTE PROCEDURE trfn_tbl_log_a();

So I have to create 26 trigger functions, one for each tbl_log_%letter%. Is there a way to tell the trigger function to:

SELECT timeidxcolumnname FROM %currenttable%

By %currenttable% I mean the table where the trigger is placed. Or:

SELECT timeidxcolumnname FROM tbl_log_%letter%

Is it possible in Postgres 9.1? Please notice that my Trigger Function, calls several variables from the table.. I'm reading about dynamically determined tables.

timeidx
fnname
timetype
stmtserial
NEW.timedix
and many more that I didn't add for size. but all in the example list all the kinds.

So Any clue? I don't know if selecting one columns fromwould like to store the current table and store it asname itself inside a variable could help me, sincenot the Trigger Function works on multiple columns inside that table and also use default postgresql internal Variable "NEW.", also if Im already using a default postgresql internal Variable "NEW." I don't know if I can add TG_TABLE_NAME and TG_TABLE_SCHEMA to that.

I guess I would like to store because the trigger function works on multiple columns inside that table name itself as a variable. Is that possible???

Thanks Advanced.

TG_TABLE_NAME
TG_TABLE_SCHEMA

I have multiple tables that have the exact same column names, they only vary in their column values:

tbl_log_a
tbl_log_b
tbl_log_c
...

26 tables (from a to z). Each table has a Trigger that calls a Trigger Function named "trfn_tbl_log_%letter% (from a to z) which does the exact same thing:

CREATE OR REPLACE FUNCTION trfn_tbl_log_a
  RETURNS trigger AS
$BODY$
DECLARE
v_timeidx real;

BEGIN

IF NEW.timetype = 'start' THEN
  SELECT timeidx FROM tbl_log_a WHERE fnname = NEW.fnname AND timetype = 'start' ORDER BY stmtserial DESC LIMIT 1 INTO v_timeidx;
    IF FOUND THEN
      NEW.timeidx := floor(v_timeidx) + 1;
    ELSE
      NEW.timeidx := 1;
    END IF;

END IF;
return NEW;

END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION trfn_tbl_log_a
  OWNER TO postgres;

So all my trigger functions are exactly the same, they only differ in the table where they select the columns (in its first IF STATEMENT):

select timeidx FROM tbl_log_a WHERE fnname...
select timeidx FROM tbl_log_b WHERE fnname...
select timeidx FROM tbl_log_c WHERE fnname...
...

My Trigger definition script is...

CREATE TRIGGER trfn_tbl_log_a
  BEFORE INSERT
  ON tbl_log_a
  FOR EACH ROW
  EXECUTE PROCEDURE trfn_tbl_log_a();

So I have to create 26 trigger functions, one for each tbl_log_%letter%. Is there a way to tell the trigger function to:

SELECT timeidx FROM %currenttable%

By %currenttable% I mean the table where the trigger is placed. Or:

SELECT timeidx FROM tbl_log_%letter%

Is it possible in Postgres 9.1? Please notice that my Trigger Function, calls several variables from the table...

timeidx
fnname
timetype
stmtserial
NEW.timedix
and many more that I didn't add for size. but all in the example list all the kinds.

So I don't know if selecting one columns from the current table and store it as a variable could help me, since the Trigger Function works on multiple columns inside that table and also use default postgresql internal Variable "NEW.", also if Im already using a default postgresql internal Variable "NEW." I don't know if I can add TG_TABLE_NAME and TG_TABLE_SCHEMA to that.

I guess I would like to store the table name itself as a variable. Is that possible???

Thanks Advanced.

I have multiple tables that have the same column names, they only vary in their column values, like:

tbl_log_a
tbl_log_b
tbl_log_c
...

26 tables from a to z. Each table has a trigger that calls a trigger function which does the exact same thing:

SELECT columnname FROM tbl_log_a

Other than that, all my trigger functions do the exact same thing. They differ in that they:

select columnname FROM tbl_log_a
select columnname FROM tbl_log_b
select columnname FROM tbl_log_c
...

So I have to create 26 trigger functions, one for each tbl_log_%letter%. Is there a way to tell the trigger function to:

SELECT columnname FROM %currenttable%

By %currenttable% I mean the table where the trigger is placed. Or:

SELECT columnname FROM tbl_log_%letter%

Is it possible in Postgres 9.1? I'm reading about dynamically determined tables. Any clue? I would like to store the table name itself inside a variable, not the columns inside that table, because the trigger function works on multiple columns inside that table.

TG_TABLE_NAME
TG_TABLE_SCHEMA
deleted 6 characters in body
Source Link
Natysiu16
  • 245
  • 2
  • 6

I have multiple tables that have the exact same column names, they only vary in their column values, like:

tbl_log_a
tbl_log_b
tbl_log_c
...

26 tables (from a to z). Each table has a Trigger that calls a Trigger Function named "trfn_tbl_log_%letter% (from a to z) which does the exact same thing:

CREATE OR REPLACE FUNCTION trfn_tbl_log_a
  RETURNS trigger AS
$BODY$
DECLARE
v_timeidx real;

BEGIN

IF NEW.timetype = 'start' THEN
  SELECT timeidx FROM tbl_log_a WHERE fnname = NEW.fnname AND timetype = 'start' ORDER BY stmtserial DESC LIMIT 1 INTO v_timeidx;
    IF FOUND THEN
      NEW.timeidx := floor(v_timeidx) + 1;
    ELSE
      NEW.timeidx := 1;
    END IF;

END IF;
return NEW;

END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION trfn_tbl_log_a
  OWNER TO postgres;

So all my trigger functions are exactly the same, they only differ in the table where they select the columns (in its first IF STATEMENT):

select timeidx FROM tbl_log_a WHERE fnname...
select timeidx FROM tbl_log_b WHERE fnname...
select timeidx FROM tbl_log_c WHERE fnname...
...

My Trigger definition script is...

CREATE TRIGGER trfn_tbl_log_a
  BEFORE INSERT
  ON tbl_log_a
  FOR EACH ROW
  EXECUTE PROCEDURE trfn_tbl_log_a();

So I have to create 26 trigger functions, one for each tbl_log_%letter%. Is there a way to tell the trigger function to:

SELECT columnnametimeidx FROM %currenttable%

By %currenttable% I mean the table where the trigger is placed. Or:

SELECT columnnametimeidx FROM tbl_log_%letter%

Is it possible in Postgres 9.1? Please notice that my Trigger Function, calls several variables from the table...

timeidx
fnname
timetype
stmtserial
NEW.timedix
and many more that I didn't add for size. but all in the example list all the kinds.

So I don't know if selecting one columns from the current table and store it as a variable could help me, since the Trigger Function works on multiple columns inside that table and also use default postgresql internal Variable "NEW.", also if Im already using a default postgresql internal Variable "NEW." I don't know if I can add TG_TABLE_NAME and TG_TABLE_SCHEMA to that.

I guess I would like to store the table name itself as a variable. Is that possible???

Thanks Advanced.

I have multiple tables that have the same column names, they only vary in their column values, like:

tbl_log_a
tbl_log_b
tbl_log_c
...

26 tables (from a to z). Each table has a Trigger that calls a Trigger Function named "trfn_tbl_log_%letter% (from a to z) which does the exact same thing:

CREATE OR REPLACE FUNCTION trfn_tbl_log_a
  RETURNS trigger AS
$BODY$
DECLARE
v_timeidx real;

BEGIN

IF NEW.timetype = 'start' THEN
  SELECT timeidx FROM tbl_log_a WHERE fnname = NEW.fnname AND timetype = 'start' ORDER BY stmtserial DESC LIMIT 1 INTO v_timeidx;
    IF FOUND THEN
      NEW.timeidx := floor(v_timeidx) + 1;
    ELSE
      NEW.timeidx := 1;
    END IF;

END IF;
return NEW;

END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION trfn_tbl_log_a
  OWNER TO postgres;

So all my trigger functions are exactly the same, they only differ in the table where they select the columns (in its first IF STATEMENT):

select timeidx FROM tbl_log_a WHERE fnname...
select timeidx FROM tbl_log_b WHERE fnname...
select timeidx FROM tbl_log_c WHERE fnname...
...

So I have to create 26 trigger functions, one for each tbl_log_%letter%. Is there a way to tell the trigger function to:

SELECT columnname FROM %currenttable%

By %currenttable% I mean the table where the trigger is placed. Or:

SELECT columnname FROM tbl_log_%letter%

Is it possible in Postgres 9.1? Please notice that my Trigger Function, calls several variables from the table...

timeidx
fnname
timetype
stmtserial
NEW.timedix
and many more that I didn't add for size. but all in the example list all the kinds.

So I don't know if selecting one columns from the current table and store it as a variable could help me, since the Trigger Function works on multiple columns inside that table and also use default postgresql internal Variable "NEW.", also if Im already using a default postgresql internal Variable "NEW." I don't know if I can add TG_TABLE_NAME and TG_TABLE_SCHEMA to that.

I guess I would like to store the table name itself as a variable. Is that possible???

Thanks Advanced.

I have multiple tables that have the exact same column names, they only vary in their column values:

tbl_log_a
tbl_log_b
tbl_log_c
...

26 tables (from a to z). Each table has a Trigger that calls a Trigger Function named "trfn_tbl_log_%letter% (from a to z) which does the exact same thing:

CREATE OR REPLACE FUNCTION trfn_tbl_log_a
  RETURNS trigger AS
$BODY$
DECLARE
v_timeidx real;

BEGIN

IF NEW.timetype = 'start' THEN
  SELECT timeidx FROM tbl_log_a WHERE fnname = NEW.fnname AND timetype = 'start' ORDER BY stmtserial DESC LIMIT 1 INTO v_timeidx;
    IF FOUND THEN
      NEW.timeidx := floor(v_timeidx) + 1;
    ELSE
      NEW.timeidx := 1;
    END IF;

END IF;
return NEW;

END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION trfn_tbl_log_a
  OWNER TO postgres;

So all my trigger functions are exactly the same, they only differ in the table where they select the columns (in its first IF STATEMENT):

select timeidx FROM tbl_log_a WHERE fnname...
select timeidx FROM tbl_log_b WHERE fnname...
select timeidx FROM tbl_log_c WHERE fnname...
...

My Trigger definition script is...

CREATE TRIGGER trfn_tbl_log_a
  BEFORE INSERT
  ON tbl_log_a
  FOR EACH ROW
  EXECUTE PROCEDURE trfn_tbl_log_a();

So I have to create 26 trigger functions, one for each tbl_log_%letter%. Is there a way to tell the trigger function to:

SELECT timeidx FROM %currenttable%

By %currenttable% I mean the table where the trigger is placed. Or:

SELECT timeidx FROM tbl_log_%letter%

Is it possible in Postgres 9.1? Please notice that my Trigger Function, calls several variables from the table...

timeidx
fnname
timetype
stmtserial
NEW.timedix
and many more that I didn't add for size. but all in the example list all the kinds.

So I don't know if selecting one columns from the current table and store it as a variable could help me, since the Trigger Function works on multiple columns inside that table and also use default postgresql internal Variable "NEW.", also if Im already using a default postgresql internal Variable "NEW." I don't know if I can add TG_TABLE_NAME and TG_TABLE_SCHEMA to that.

I guess I would like to store the table name itself as a variable. Is that possible???

Thanks Advanced.

added 973 characters in body
Source Link
Natysiu16
  • 245
  • 2
  • 6

I have multiple tables that have the same column names, they only vary in their column values, like:

tbl_log_a
tbl_log_b
tbl_log_c
...

26 tables (from a to z). Each table has a Trigger that calls a Trigger Function named "trfn_tbl_log_%letter% (from a to z) which does the exact same thing:

CREATE OR REPLACE FUNCTION trfn_tbl_log_a
  RETURNS trigger AS
$BODY$
DECLARE
v_timeidx real;

BEGIN

IF NEW.timetype = 'start' THEN
  SELECT timeidx FROM tbl_log_a WHERE fnname = NEW.fnname AND timetype = 'start' ORDER BY stmtserial DESC LIMIT 1 INTO v_timeidx;
    IF FOUND THEN
      NEW.timeidx := floor(v_timeidx) + 1;
    ELSE
      NEW.timeidx := 1;
    END IF;

END IF;
return NEW;

END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION trfn_tbl_log_a
  OWNER TO postgres;

So all my trigger functions are exactly the same, they only differ in thatthe table where they select the columns (in its first IF STATEMENT):

select columnnametimeidx FROM tbl_log_a WHERE fnname...
select columnnametimeidx FROM tbl_log_b WHERE fnname...
select columnnametimeidx FROM tbl_log_c WHERE fnname...
...

So I have to create 26 trigger functions, one for each tbl_log_%letter%. Is there a way to tell the trigger function to:

SELECT columnname FROM %currenttable%

By %currenttable% I mean the table where the trigger is placed. Or:

SELECT columnname FROM tbl_log_%letter%

Is it possible in Postgres 9.1? Please notice that my Trigger Function, calls several variables from the table...

timeidx
fnname
timetype
stmtserial
NEW.timedix
and many more that I didn't add for size. but all in the example list all the kinds.

So I don't know selectif selecting one columns from the current table and store it as a variable could help me, since the Trigger Function works on multiple columns inside that table and also use default postgresql internal Variable "NEW.", also if Im already using a default postgresql internal Variable "NEW." so I don't know if I can add TG_TABLE_NAME and TG_TABLE_SCHEMA to that.

I guess I would like to store the table name itself insideas a variable. Is that possible???

Thanks Advanced.

I have multiple tables that have the same column names, they only vary in their column values, like:

tbl_log_a
tbl_log_b
tbl_log_c
...

26 tables (from a to z). Each table has a Trigger that calls a Trigger Function named "trfn_tbl_log_%letter% (from a to z) which does the exact same thing:

CREATE OR REPLACE FUNCTION trfn_tbl_log_a
  RETURNS trigger AS
$BODY$
DECLARE
v_timeidx real;

BEGIN

IF NEW.timetype = 'start' THEN
  SELECT timeidx FROM tbl_log_a WHERE fnname = NEW.fnname AND timetype = 'start' ORDER BY stmtserial DESC LIMIT 1 INTO v_timeidx;
    IF FOUND THEN
      NEW.timeidx := floor(v_timeidx) + 1;
    ELSE
      NEW.timeidx := 1;
    END IF;

END IF;
return NEW;

END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION trfn_tbl_log_a
  OWNER TO postgres;

So all my trigger functions are exactly the same, they only differ in that they (in its first IF STATEMENT):

select columnname FROM tbl_log_a
select columnname FROM tbl_log_b
select columnname FROM tbl_log_c
...

So I have to create 26 trigger functions, one for each tbl_log_%letter%. Is there a way to tell the trigger function to:

SELECT columnname FROM %currenttable%

By %currenttable% I mean the table where the trigger is placed. Or:

SELECT columnname FROM tbl_log_%letter%

Is it possible in Postgres 9.1? Please notice that my Trigger Function, calls several variables from the table...

timeidx
fnname
timetype
stmtserial
NEW.timedix
and many more that I didn't add for size. but all in the example list all the kinds.

So I don't know select one columns from the current table and store it as a variable could help me, since the Trigger Function works on multiple columns inside that table. also Im already using a default postgresql internal Variable "NEW." so I don't know if I can add TG_TABLE_NAME and TG_TABLE_SCHEMA to that.

I guess I would like to store the table name itself inside a variable. Is that possible???

Thanks Advanced.

I have multiple tables that have the same column names, they only vary in their column values, like:

tbl_log_a
tbl_log_b
tbl_log_c
...

26 tables (from a to z). Each table has a Trigger that calls a Trigger Function named "trfn_tbl_log_%letter% (from a to z) which does the exact same thing:

CREATE OR REPLACE FUNCTION trfn_tbl_log_a
  RETURNS trigger AS
$BODY$
DECLARE
v_timeidx real;

BEGIN

IF NEW.timetype = 'start' THEN
  SELECT timeidx FROM tbl_log_a WHERE fnname = NEW.fnname AND timetype = 'start' ORDER BY stmtserial DESC LIMIT 1 INTO v_timeidx;
    IF FOUND THEN
      NEW.timeidx := floor(v_timeidx) + 1;
    ELSE
      NEW.timeidx := 1;
    END IF;

END IF;
return NEW;

END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION trfn_tbl_log_a
  OWNER TO postgres;

So all my trigger functions are exactly the same, they only differ in the table where they select the columns (in its first IF STATEMENT):

select timeidx FROM tbl_log_a WHERE fnname...
select timeidx FROM tbl_log_b WHERE fnname...
select timeidx FROM tbl_log_c WHERE fnname...
...

So I have to create 26 trigger functions, one for each tbl_log_%letter%. Is there a way to tell the trigger function to:

SELECT columnname FROM %currenttable%

By %currenttable% I mean the table where the trigger is placed. Or:

SELECT columnname FROM tbl_log_%letter%

Is it possible in Postgres 9.1? Please notice that my Trigger Function, calls several variables from the table...

timeidx
fnname
timetype
stmtserial
NEW.timedix
and many more that I didn't add for size. but all in the example list all the kinds.

So I don't know if selecting one columns from the current table and store it as a variable could help me, since the Trigger Function works on multiple columns inside that table and also use default postgresql internal Variable "NEW.", also if Im already using a default postgresql internal Variable "NEW." I don't know if I can add TG_TABLE_NAME and TG_TABLE_SCHEMA to that.

I guess I would like to store the table name itself as a variable. Is that possible???

Thanks Advanced.

added 973 characters in body
Source Link
Natysiu16
  • 245
  • 2
  • 6
Loading
clean up, adapt tags
Source Link
Erwin Brandstetter
  • 182.1k
  • 28
  • 457
  • 620
Loading
added 34 characters in body
Source Link
Natysiu16
  • 245
  • 2
  • 6
Loading
added 174 characters in body
Source Link
Natysiu16
  • 245
  • 2
  • 6
Loading
edited tags
Link
Craig Ringer
  • 57.3k
  • 6
  • 159
  • 192
Loading
added 40 characters in body
Source Link
Natysiu16
  • 245
  • 2
  • 6
Loading
deleted 94 characters in body
Source Link
Natysiu16
  • 245
  • 2
  • 6
Loading
added 125 characters in body
Source Link
Natysiu16
  • 245
  • 2
  • 6
Loading
Source Link
Natysiu16
  • 245
  • 2
  • 6
Loading