Postgresql 9.4: How to find out the stack level of nested function

functionspostgresql

Here are the two cases:

  • Function fA fetches some rows and returns a table
  • Function fB calls function fA and returns a table

Within fA, how could I know whether it's been called from the program or by fB?

Is there some @@nestlevel that can count how deep in the stack are the functions running?

Big thank you in advance,

Best Answer

There's a different, simpler, solution for this. Add an argument to function fA with a default value, for instance: called_from_b BOOLEAN DEFAULT FALSE. Then, provide that argument when calling fA from fB.

I understand that this doesn't directly answer your question, but it does provide a solution for the example usecase that you provide.